openapi-typescript
openapi-typescript copied to clipboard
Empty body should not result in Content-Type header being set
Description
Any HTTP method is free to choose whether it sends a payload or not (GET might be debatable, but that's not important).
When it sends one, it should declare what type it is, by providing Content-Type
header. When it doesn't send one, it MUST NOT declare the header.
The current implementation declares application/json
for every call, even GET
without specifying any body:
We are currently using a workaround through a middleware:
client.use({
onRequest: req => {
if (req.body === null) {
req.headers.delete('Content-Type')
}
return req
},
})
I know that there are at least two closed issues related to this, but I want to emphasize that this indeed is a bug which makes the library non-compliant with rules of HTTP protocol. I really like the library and I would like to remove this little stain to make it shine even more.
Reproduction
Just fire a POST request against a strict API server, such as Fastify.
await client.POST('/some-endpoint')
and see it respond with 400 Bad Request:
Body cannot be empty when content-type is set to 'application/json'
Fastify rightfully complains about the malformed incoming request.
Expected result
There should be no Content-Type
header present, when we're not sending any body - regardless of the HTTP method.
Checklist
- [x] I’m willing to open a PR (see CONTRIBUTING.md)