title: Guzzle - cURL error 18: transfer closed with outstanding read data remaining
body: <p>I'm trying to solve a problem with my call to external API. With Postman everything is working fine. When I'm trying to use Guzzle, this error appears.</p>
  <p>I already tried using HTTP version 1.0, but it just cuts my response and I'm not receiving proper JSON. There is also no Content-Length header in response.</p>
  <p>Any advice is appreciated.</p>
  <p>Code:</p>
  <pre><code>private const TIMEOUT = 90;
  private const CONNECT_TIMEOUT = 90;
  private const SSL_VERIFY = false;
  
  // ...
  
  private function getDefaultHeaders(): array
  {
      $headers = [
          'accept' =&gt; '*/*',
          'Content-Type' =&gt; 'application/json',
          'Accept-Encoding' =&gt; 'gzip, deflate',
          'Connection' =&gt; 'keep-alive',
      ];
  
      if ($this-&gt;authorizationProvider-&gt;isAuthorizationEnabled()) {
          $headers['Authorization'] = $this-&gt;authorizationProvider-&gt;getAuthorizationHeader();
      }
  
      return $headers;
  }
  
  // ...
  
  $response = $this-&gt;client-&gt;request(
      Request::METHOD_POST,
      $uri,
      [
          'body' =&gt; json_encode($body),
          'headers' =&gt; $this-&gt;getHeaders(),
          'timeout' =&gt; self::TIMEOUT,
          'connect_timeout' =&gt; self::CONNECT_TIMEOUT,
          'verify' =&gt; self::SSL_VERIFY,
      ]
  );
  </code></pre>
  <p>Exception:</p>
  <pre><code>GuzzleHttp\Exception\RequestException {#494
    -request: GuzzleHttp\Psr7\Request {#488
      -method: &quot;GET&quot;
      -requestTarget: null
      -uri: GuzzleHttp\Psr7\Uri {#484
        -scheme: &quot;https&quot;
        -userInfo: &quot;&quot;
        -host: &quot;arch.xxx.com&quot;
        -port: null
        -path: &quot;/api/1/xxx/123456/xxx&quot;
        -query: &quot;&quot;
        -fragment: &quot;&quot;
      }
      -headers: array:6 [
        &quot;User-Agent&quot; =&gt; array:1 [
          0 =&gt; &quot;GuzzleHttp/6.5.1 curl/7.52.1 PHP/7.2.34&quot;
        ]
        &quot;Host&quot; =&gt; array:1 [
          0 =&gt; &quot;arch.xxx.com&quot;
        ]
        &quot;accept&quot; =&gt; array:1 [
          0 =&gt; &quot;application/json&quot;
        ]
        &quot;Content-Type&quot; =&gt; array:1 [
          0 =&gt; &quot;application/json&quot;
        ]
        &quot;accept-encoding&quot; =&gt; array:1 [
          0 =&gt; &quot;gzip, deflate&quot;
        ]
        &quot;Expect&quot; =&gt; array:1 [
          0 =&gt; &quot;100-continue&quot;
        ]
      ]
      -headerNames: array:6 [
        &quot;user-agent&quot; =&gt; &quot;User-Agent&quot;
        &quot;host&quot; =&gt; &quot;Host&quot;
        &quot;accept&quot; =&gt; &quot;accept&quot;
        &quot;content-type&quot; =&gt; &quot;Content-Type&quot;
        &quot;accept-encoding&quot; =&gt; &quot;accept-encoding&quot;
        &quot;expect&quot; =&gt; &quot;Expect&quot;
      ]
      -protocol: &quot;1.1&quot;
      -stream: GuzzleHttp\Psr7\Stream {#487
        -stream: stream resource @329
          wrapper_type: &quot;PHP&quot;
          stream_type: &quot;TEMP&quot;
          mode: &quot;w+b&quot;
          unread_bytes: 0
          seekable: true
          uri: &quot;php://temp&quot;
          options: []
        }
        -size: 0
        -seekable: true
        -readable: true
        -writable: true
        -uri: &quot;php://temp&quot;
        -customMetadata: []
      }
    }
    -response: GuzzleHttp\Psr7\Response {#495
      -reasonPhrase: &quot;OK&quot;
      -statusCode: 200
      -headers: array:20 [
        &quot;Content-Type&quot; =&gt; array:1 [
          0 =&gt; &quot;application/json&quot;
        ]
        &quot;Access-Control-Allow-Origin&quot; =&gt; array:1 [
          0 =&gt; &quot;*&quot;
        ]
        &quot;Access-Control-Allow-Headers&quot; =&gt; array:1 [
          0 =&gt; &quot;DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Content-Range,Range,soapaction,dnt,x-customheader,keep-alive,user-agent,x-requested-with,if-modified-since,cache-control,content-type,content-range,range,X-NewRelic-ID,x-newrelic-id&quot;
        ]
        &quot;Access-Control-Allow-Methods&quot; =&gt; array:1 [
          0 =&gt; &quot;GET, POST, OPTIONS&quot;
        ]
        &quot;Access-Control-Expose-Headers&quot; =&gt; array:1 [
          0 =&gt; &quot;*&quot;
        ]
        &quot;Access-Control-Max-Age&quot; =&gt; array:1 [
          0 =&gt; &quot;1728000&quot;
        ]
        &quot;X-Frame-Options&quot; =&gt; array:1 [
          0 =&gt; &quot;SAMEORIGIN&quot;
        ]
        &quot;X-Content-Type-Options&quot; =&gt; array:1 [
          0 =&gt; &quot;nosniff&quot;
        ]
        &quot;X-XSS-Protection&quot; =&gt; array:1 [
          0 =&gt; &quot;1; mode=block&quot;
        ]
        &quot;X-UA-Compatible&quot; =&gt; array:1 [
          0 =&gt; &quot;IE=edge,chrome=1&quot;
        ]
        &quot;Accept-Ranges&quot; =&gt; array:1 [
          0 =&gt; &quot;bytes&quot;
        ]
        &quot;Vary&quot; =&gt; array:1 [
          0 =&gt; &quot;Accept-Encoding&quot;
        ]
        &quot;Expires&quot; =&gt; array:1 [
          0 =&gt; &quot;Fri, 05 Nov 2021 14:23:24 GMT&quot;
        ]
        &quot;Cache-Control&quot; =&gt; array:1 [
          0 =&gt; &quot;max-age=0, no-cache, no-store&quot;
        ]
        &quot;Pragma&quot; =&gt; array:1 [
          0 =&gt; &quot;no-cache&quot;
        ]
        &quot;Date&quot; =&gt; array:1 [
          0 =&gt; &quot;Fri, 05 Nov 2021 14:23:24 GMT&quot;
        ]
        &quot;Transfer-Encoding&quot; =&gt; array:1 [
          0 =&gt; &quot;chunked&quot;
        ]
        &quot;Connection&quot; =&gt; array:2 [
          0 =&gt; &quot;keep-alive&quot;
          1 =&gt; &quot;Transfer-Encoding&quot;
        ]
        &quot;Server-Timing&quot; =&gt; array:3 [
          0 =&gt; &quot;cdn-cache; desc=MISS&quot;
          1 =&gt; &quot;edge; dur=23&quot;
          2 =&gt; &quot;origin; dur=10997&quot;
        ]
        &quot;x-encoded-content-encoding&quot; =&gt; array:1 [
          0 =&gt; &quot;gzip&quot;
        ]
      ]
      -headerNames: array:20 [
        &quot;content-type&quot; =&gt; &quot;Content-Type&quot;
        &quot;access-control-allow-origin&quot; =&gt; &quot;Access-Control-Allow-Origin&quot;
        &quot;access-control-allow-headers&quot; =&gt; &quot;Access-Control-Allow-Headers&quot;
        &quot;access-control-allow-methods&quot; =&gt; &quot;Access-Control-Allow-Methods&quot;
        &quot;access-control-expose-headers&quot; =&gt; &quot;Access-Control-Expose-Headers&quot;
        &quot;access-control-max-age&quot; =&gt; &quot;Access-Control-Max-Age&quot;
        &quot;x-frame-options&quot; =&gt; &quot;X-Frame-Options&quot;
        &quot;x-content-type-options&quot; =&gt; &quot;X-Content-Type-Options&quot;
        &quot;x-xss-protection&quot; =&gt; &quot;X-XSS-Protection&quot;
        &quot;x-ua-compatible&quot; =&gt; &quot;X-UA-Compatible&quot;
        &quot;accept-ranges&quot; =&gt; &quot;Accept-Ranges&quot;
        &quot;vary&quot; =&gt; &quot;Vary&quot;
        &quot;expires&quot; =&gt; &quot;Expires&quot;
        &quot;cache-control&quot; =&gt; &quot;Cache-Control&quot;
        &quot;pragma&quot; =&gt; &quot;Pragma&quot;
        &quot;date&quot; =&gt; &quot;Date&quot;
        &quot;transfer-encoding&quot; =&gt; &quot;Transfer-Encoding&quot;
        &quot;connection&quot; =&gt; &quot;Connection&quot;
        &quot;server-timing&quot; =&gt; &quot;Server-Timing&quot;
        &quot;x-encoded-content-encoding&quot; =&gt; &quot;x-encoded-content-encoding&quot;
      ]
      -protocol: &quot;1.1&quot;
      -stream: GuzzleHttp\Psr7\Stream {#493
        -stream: stream resource @331
          wrapper_type: &quot;PHP&quot;
          stream_type: &quot;TEMP&quot;
          mode: &quot;w+b&quot;
          unread_bytes: 0
          seekable: true
          uri: &quot;php://temp&quot;
          options: []
        }
        -size: null
        -seekable: true
        -readable: true
        -writable: true
        -uri: &quot;php://temp&quot;
        -customMetadata: []
      }
    }
    -handlerContext: array:30 [
      &quot;errno&quot; =&gt; 18
      &quot;error&quot; =&gt; &quot;transfer closed with outstanding read data remaining&quot;
      &quot;appconnect_time&quot; =&gt; 0.042203
      &quot;url&quot; =&gt; &quot;https://arch.xxx.com/api/1/xxx/123456/xxx&quot;
      &quot;content_type&quot; =&gt; &quot;application/json&quot;
      &quot;http_code&quot; =&gt; 200
      &quot;header_size&quot; =&gt; 1040
      &quot;request_size&quot; =&gt; 237
      &quot;filetime&quot; =&gt; -1
      &quot;ssl_verify_result&quot; =&gt; 0
      &quot;redirect_count&quot; =&gt; 0
      &quot;total_time&quot; =&gt; 11.115178
      &quot;namelookup_time&quot; =&gt; 0.012257
      &quot;connect_time&quot; =&gt; 0.020937
      &quot;pretransfer_time&quot; =&gt; 0.042225
      &quot;size_upload&quot; =&gt; 0.0
      &quot;size_download&quot; =&gt; 52470.0
      &quot;speed_download&quot; =&gt; 4720.0
      &quot;speed_upload&quot; =&gt; 0.0
      &quot;download_content_length&quot; =&gt; -1.0
      &quot;upload_content_length&quot; =&gt; -1.0
      &quot;starttransfer_time&quot; =&gt; 11.101096
      &quot;redirect_time&quot; =&gt; 0.0
      &quot;redirect_url&quot; =&gt; &quot;&quot;
      &quot;primary_ip&quot; =&gt; &quot;2.1.2.1&quot;
      &quot;certinfo&quot; =&gt; []
      &quot;primary_port&quot; =&gt; 443
      &quot;local_ip&quot; =&gt; &quot;192.168.200.181&quot;
      &quot;local_port&quot; =&gt; 33130
      &quot;curl_version&quot; =&gt; &quot;7.52.1&quot;
    ]
    #message: &quot;cURL error 18: transfer closed with outstanding read data remaining (see https://curl.haxx.se/libcurl/c/libcurl-errors.html)&quot;
    #code: 200
    #file: &quot;/var/www/api/vendor/guzzlehttp/guzzle/src/Handler/CurlFactory.php&quot;
    #line: 201
    trace: {
      /var/www/api/vendor/guzzlehttp/guzzle/src/Handler/CurlFactory.php:201 {}
      /var/www/api/vendor/guzzlehttp/guzzle/src/Handler/CurlFactory.php:155 {}
      /var/www/api/vendor/guzzlehttp/guzzle/src/Handler/CurlFactory.php:105 {}
      /var/www/api/vendor/guzzlehttp/guzzle/src/Handler/CurlHandler.php:43 {}
      /var/www/api/vendor/guzzlehttp/guzzle/src/Handler/Proxy.php:28 {}
      /var/www/api/vendor/guzzlehttp/guzzle/src/Handler/Proxy.php:51 {}
      /var/www/api/vendor/guzzlehttp/guzzle/src/PrepareBodyMiddleware.php:37 {}
      /var/www/api/vendor/guzzlehttp/guzzle/src/Middleware.php:29 {}
      /var/www/api/vendor/guzzlehttp/guzzle/src/RedirectMiddleware.php:70 {}
      /var/www/api/vendor/guzzlehttp/guzzle/src/Middleware.php:59 {}
      /var/www/api/vendor/guzzlehttp/guzzle/src/HandlerStack.php:71 {}
      /var/www/api/vendor/guzzlehttp/guzzle/src/Client.php:361 {}
      /var/www/api/vendor/guzzlehttp/guzzle/src/Client.php:163 {}
      /var/www/api/vendor/guzzlehttp/guzzle/src/Client.php:183 {}
      /var/www/api/src/RestConnectorService.php:126 {
        Api\RestConnectorService-&gt;sendGetRequest(string $uri): ResponseInterface …
        ›     'connect_timeout' =&gt; self::CONNECT_TIMEOUT,
        ›     'verify' =&gt; self::SSL_VERIFY,
        › ]
        arguments: {
          $method: &quot;GET&quot;
          $uri: &quot;https://arch.xxx.com/api/1/xxx/123456/xxx?&quot;
          $options: array:5 [ …5]
        }
      }el/HttpKernel.php:68 {}
      /var/www/api/vendor/symfony/http-kernel/Kernel.php:201 {}
      /var/www/api/public/index.php:25 {}
    }
  }
  </code></pre>