@@ -197,8 +197,14 @@ public function request(string $method, string $url, array $options = []): Respo
197197 if (\extension_loaded ('zlib ' ) && !isset ($ options ['normalized_headers ' ]['accept-encoding ' ])) {
198198 $ options ['headers ' ][] = 'Accept-Encoding: gzip ' ; // Expose only one encoding, some servers mess up when more are provided
199199 }
200+ $ body = $ options ['body ' ];
200201
201- foreach ($ options ['headers ' ] as $ header ) {
202+ foreach ($ options ['headers ' ] as $ i => $ header ) {
203+ if (\is_string ($ body ) && '' !== $ body && 0 === stripos ($ header , 'Content-Length: ' )) {
204+ // Let curl handle Content-Length headers
205+ unset($ options ['headers ' ][$ i ]);
206+ continue ;
207+ }
202208 if (': ' === $ header [-2 ] && \strlen ($ header ) - 2 === strpos ($ header , ': ' )) {
203209 // curl requires a special syntax to send empty headers
204210 $ curlopts [\CURLOPT_HTTPHEADER ][] = substr_replace ($ header , '; ' , -2 );
@@ -214,7 +220,7 @@ public function request(string $method, string $url, array $options = []): Respo
214220 }
215221 }
216222
217- if (!\is_string ($ body = $ options [ ' body ' ] )) {
223+ if (!\is_string ($ body )) {
218224 if (\is_resource ($ body )) {
219225 $ curlopts [\CURLOPT_INFILE ] = $ body ;
220226 } else {
@@ -226,15 +232,16 @@ public function request(string $method, string $url, array $options = []): Respo
226232 }
227233
228234 if (isset ($ options ['normalized_headers ' ]['content-length ' ][0 ])) {
229- $ curlopts [\CURLOPT_INFILESIZE ] = substr ($ options ['normalized_headers ' ]['content-length ' ][0 ], \strlen ('Content-Length: ' ));
230- } elseif (!isset ($ options ['normalized_headers ' ]['transfer-encoding ' ])) {
231- $ curlopts [\CURLOPT_HTTPHEADER ][] = 'Transfer-Encoding: chunked ' ; // Enable chunked request bodies
235+ $ curlopts [\CURLOPT_INFILESIZE ] = (int ) substr ($ options ['normalized_headers ' ]['content-length ' ][0 ], \strlen ('Content-Length: ' ));
236+ }
237+ if (!isset ($ options ['normalized_headers ' ]['transfer-encoding ' ])) {
238+ $ curlopts [\CURLOPT_HTTPHEADER ][] = 'Transfer-Encoding: ' .(isset ($ curlopts [\CURLOPT_INFILESIZE ]) ? '' : ' chunked ' );
232239 }
233240
234241 if ('POST ' !== $ method ) {
235242 $ curlopts [\CURLOPT_UPLOAD ] = true ;
236243
237- if (!isset ($ options ['normalized_headers ' ]['content-type ' ])) {
244+ if (!isset ($ options ['normalized_headers ' ]['content-type ' ]) && 0 !== ( $ curlopts [\ CURLOPT_INFILESIZE ] ?? null ) ) {
238245 $ curlopts [\CURLOPT_HTTPHEADER ][] = 'Content-Type: application/x-www-form-urlencoded ' ;
239246 }
240247 }
0 commit comments