yiisoft/yii2-httpclient

Set "Content-Length: 0" request header when request body is null on POST requests

beroso opened this issue · 2 comments

I'm using the CurlTransport, so maybe it's related to my cURL version.
I needed to make a post request to a API without body params, and the server responded with 400 status code.
It seems that HTTP POST requests requires a Content-Length header.

To overcome the problem, I sent a empty string as data:

$invoiceId = 'E58046425FA3472089A67A417D2AEF5D';
// ...
    ->setUrl('https://example.api/v1/' . $invoiceId . '/send_email')
    ->setMethod('post')
    ->setData('')
    ->send();
// ...

But it smells like a workaround to me :) .

I think it can be automatically set when preparing the request, to avoid the cURL bug.

Additional info:
cURL version: 7.29.0
PHP: 7.1.13

That's correct.

HTTP headers sent before:

POST /user/test HTTP/1.1
Host: app.test
Accept: */*
Content-Type: application/x-www-form-urlencoded; charset=UTF-8
Expect: 100-continue

after the fix:

POST /user/test HTTP/1.1
Host: app.test
Accept: */*
Content-Type: application/x-www-form-urlencoded; charset=UTF-8
Content-Length: 0