stoplightio / elements

Build beautiful, interactive API Docs with embeddable React or Web Components, powered by OpenAPI and Markdown.
https://stoplight.io/open-source/elements/
Apache License 2.0
1.82k stars 206 forks source link

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

Open nikrooz opened 1 week ago

nikrooz commented 1 week ago

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

mnaumanali94 commented 4 days ago

@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 commented 4 days ago

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 https://github.com/stoplightio/elements/pull/2739, please have a look when you have a moment. Thanks for your help.