Skip to content

DELETE request fails with "Body cannot be empty when content-type is set to 'application/json'" #1477

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
1 task
kapetr opened this issue Dec 4, 2023 · 2 comments
Labels
bug Something isn't working openapi-fetch Relevant to the openapi-fetch library

Comments

@kapetr
Copy link

kapetr commented Dec 4, 2023

Description

We are not able to run DELETE requests (or POST request with empty body), because the library always adds content-type: 'application/json' header and api doesn't consider an empty body a valid json (returns Body cannot be empty when content-type is set to 'application/json). Or is there some option in openAPI schema I'm missing to remove this? I was able to hack this by explicitly including content-type: 'text-plain' header to every DELETE request.

Example of the delete chunk in schema (simplified):

{
  "files/{id}": {
    "delete": {
      "parameters": [
        {
          "schema": {
            "type": "string"
          },
          "in": "path",
          "name": "id",
          "required": true
        }
      ],
      "responses": {
        "204": {
          "description": "Success"
        }
      }
    }
  }
}

Reproduction

Expected result

Checklist

@kapetr kapetr added bug Something isn't working openapi-fetch Relevant to the openapi-fetch library labels Dec 4, 2023
@drwpow
Copy link
Contributor

drwpow commented Dec 14, 2023

The error message Body cannot be empty when content-type is set to 'application/json' is unique to your endpoint, so I don’t think there’s a good default that can be changed here (for most endpoints, Content-Type: application/json helps endpoints return the correct response back as a default).

Also you can unset that default header for all endpoints with:

createClient({ headers: { 'Content-Type': null } });

But yes if you wanted to do something more complex such as send application/json for all endpoints by default, but omit that header for DELETE requests, then that’d probably have to be solved with upcoming middleware support (#1122)

@drwpow
Copy link
Contributor

drwpow commented Mar 6, 2024

Closing this issue as I think between the provided workaround, and new support for middleware, there are ways to solve this! But please correct me if I’m missing anything.

@drwpow drwpow closed this as completed Mar 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working openapi-fetch Relevant to the openapi-fetch library
Projects
None yet
Development

No branches or pull requests

2 participants