Node-fetch: Content-Length header not sent with DELETE request with a body

Created on 8 Apr 2016  路  4Comments  路  Source: node-fetch/node-fetch

After some troubleshooting with an API integration test I where node-fetch was being used I found that the body of my request in Express was showing up as Object without any of the properties that I has specified originally on the JSON body with the request. Looking into a bit it appears that DELETE requests are generally allowed to have a request body although it's debatable whether it is correct to use it as seen by the behavior in Angular. After digging into the problem by looking into any reported issues for the body-parser package typically used with Express it was pointed out that Content-Type and Content-Length should be specified. Looking into my request with the debugger I could see that the Content-Length field was not being filled in for DELETEs so I added it manually to the headers of the request and now body-parser seems happy and everything appears to work as expected.

Should Content-Length be set automatically with DELETEs as it seems to be with other request verbs?

Most helpful comment

It seems this would be fixed by removing the check options.method.substr(0, 1).toUpperCase() === 'P' before automatically setting the content-length

All 4 comments

Depends on how you pass the body, content-length may or may not be set. But the only instance where we actively set content-length, is when you pass it a form-data body. Anything else is managed by native http api.

For example, if you pass node-fetch a stream as body, the content-length is never set, regardless of methods. But passing it a string, http.request will happily add the content-length for you.

I got more time to look at this today and do find we set content-length less consistently than I would hope to.

https://github.com/bitinn/node-fetch/blob/master/index.js#L91-L101

I think this could be added for a future release, doesn't seem necessary to exclude DELETE.

It seems this would be fixed by removing the check options.method.substr(0, 1).toUpperCase() === 'P' before automatically setting the content-length

DELETE request now sets content-length IF your body is a string (you need to set content-length manually if you use stream as body, as it is with other request type)

released as v1.5.3

Was this page helpful?
0 / 5 - 0 ratings

Related issues

theogravity picture theogravity  路  5Comments

Richienb picture Richienb  路  4Comments

tarikjn picture tarikjn  路  3Comments

bittlingmayer picture bittlingmayer  路  4Comments

roccomuso picture roccomuso  路  3Comments