Skip to content

Empty body should not result in Content-Type header being set #1694

Closed
@goce-cz

Description

@goce-cz

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    PRs welcomePRs are welcome to solve this issue!bugSomething isn't workinggood first issueStraightforward problem, solvable for first-time contributors without deep knowledge of the projectopenapi-fetchRelevant to the openapi-fetch library

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions