Skip to content
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

Content-Type header added despite no request body defined in POST/PUT/PATCH endpoints #2734

Open
nikrooz opened this issue Nov 15, 2024 · 2 comments

Comments

@nikrooz
Copy link

nikrooz commented Nov 15, 2024

Context

This bug affects POST/PUT/PATCH endpoints that lack a request body. When a request is made, a Content-Type: application/json header is added, which can cause an error in the API as it attempts to decode a nonexistent request body.

Current Behavior

The "Try-it" feature and example snippets add a Content-Type: application/json header to the request, even when no request body is specified in the spec for POST/PUT/PATCH endpoints.

Expected Behavior

If no request body is defined, the Content-Type header should not be included.

Possible Workaround/Solution

Steps to Reproduce

  1. Use this spec openapi.json in https://elements-demo.stoplight.io/
  2. You'll see the example curl snippet include Content-Type
Screenshot 2024-11-15 at 10 30 13 3. And the request from Try it includes `Content-Type` header

Environment

  • Version used: 8.4.7
  • Environment name and version (e.g. Chrome 39, node.js 5.4):
  • Operating System and version (desktop or mobile):
  • Link to your environment/workspace/project:
@mnaumanali94
Copy link
Contributor

@nikrooz Happy to review if you create a PR. Out of curiosity what is the usecase for a PUT/POST without a request body?

@nikrooz
Copy link
Author

nikrooz commented Nov 22, 2024

Thanks @mnaumanali94.

I'm not an expert in API design, but while it's uncommon to use a POST request without a body, it can be useful in certain scenarios. For example, consider POST /workflows/{id}/run, where the request triggers a state change. In this case, using POST is ideal because it indicates a mutation, even though no additional information is needed in the body.

Our use case is slightly more nuanced. In our situation, "getting" a resource induces side effects, including some state changes. Therefore, using POST is beneficial to accurately represent these side effects in the API and ensure the correct caching behavior.

I have opened #2739, please have a look when you have a moment. Thanks for your help.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants