Set an Accept header in the HTTP request #4983
Replies: 16 comments
-
@collimarco I believe there are two reasons for that restriction:
Does that make it more clear? |
Beta Was this translation helpful? Give feedback.
-
Yes, I already know that. The point is that you may have a successful status code like 204 that doesn't expect a content in the response. So you don't have a way to specify the Accept header in the request in that case! For example: responses:
'204':
description: Resource was successfully deleted.
# there is no response body here, so we cannot specify application/json, so the Accept header is not set in the request
# so Rails and other frameworks will respond in HTML format (or a redirect in my original Rails example) This is not a Rails bug. Normally in HTTP you can specify a Accept header in the request to ask a specific response format to the API request. Not supporting that is a major bug in the OpenAPI-Specification. The current specification is mixing and confusing the Content-Type of the response with the Accept header of the request. If an API needs a specific Accept in the request in order to work properly, it should be possible to describe that with OpenAPI.
That is an additional issue with the current specification. You have an Accept header that is totally unpredictable, unordered and can be different depending on the tool implementation. The response of an API can obviously change based on the Accept header of the request: so you absolutely need to have a way to specify that in some way.
And that is an issue. They should not be free to "set whatever they want". Sometimes you need to specify (in the specification) what they MUST request in order to comply to the API contract. |
Beta Was this translation helpful? Give feedback.
-
@collimarco Why do you need I am also questioning why, given that neither OpenAPI nor Rails are new technologies, this has never come up before if this is as fundamental of a problem as you say. Is the code in your original post code that you wrote, or is it code deep in the framework that you cannot change? |
Beta Was this translation helpful? Give feedback.
-
As I wrote in the previous message, I am talking about the Accept header in the HTTP request. I am not talking about the response. I simply need to set an Accept header (application/json) in the request, so that Rails responds in the correct format. This is not currently possible with OpenAPI.
I found several reports online about this issue, but they don't have any answer.
That code is very common in Rails applications, it's generated by Rails scaffolding and is a common practice. The API has been there for many years, but we are now adding an OpenAPI specification (so we cannot change the API, also because it's not an error in the API). The API simply expects an Accept header in the request in order to answer with the correct format. |
Beta Was this translation helpful? Give feedback.
-
@collimarco yes, I know we are talking about the request. Why would you send an
So send one. I still don't understand what's stopping you from sending one. |
Beta Was this translation helpful? Give feedback.
-
In the Rails example above, if you don't send
I could add an Accept header in the request specification, but OpenAPI says that "the parameter definition SHALL be ignored" in this case. So the tools that use the OpenAPI spec would ignore my Accept header added to the request. |
Beta Was this translation helpful? Give feedback.
-
That's not what that "SHALL be ignored" means. That requirement means that a Parameter Object that has Again, adding an |
Beta Was this translation helpful? Give feedback.
-
openapi: 3.1.0
info:
title: Example API
version: 1.0.0
paths:
/posts/{id}:
delete:
summary: Delete a post
description: Deletes a post by its unique ID.
parameters:
- in: path
name: id
required: true
description: The ID of the post to delete
schema:
type: string
- in: header # will this be ignored by tools that respect the OpenAPI standard?
name: Accept
required: true
schema:
type: string
enum: [application/json]
responses:
'204':
description: Post successfully deleted (no content returned)
'404':
description: Post not found
|
Beta Was this translation helpful? Give feedback.
-
@collimarco No, you cannot have a Parameter Object with |
Beta Was this translation helpful? Give feedback.
-
@collimarco Your use case seems to be something that signatures could solve in the next major OpenAPI version aka Moonwalk, see You would define two signatures with the same URI template and HTTP method, and different values for the
Would you mind adding this use case to the signatures discussion and closing the issue here? |
Beta Was this translation helpful? Give feedback.
-
@handrews Ok, clients can set an Accept header freely, but that is not the point. I need to document in the OpenAPI specification that the client MUST always include the @ralfhandl The HTML response is meant for browsers. The JSON response is meant for the API, and I need to document only this in the OpenAPI spec. I don't need to document the two versions. |
Beta Was this translation helpful? Give feedback.
-
@collimarco Even if you only want to document the JSON response in the OpenAPI Description of your API, that is not possible with OpenAPI 3.x and planned for OpenAPI 4.0, which is why I kindly ask you to add your use case to the 4.0 discussion and close this 3.x issue. Thanks in advance! |
Beta Was this translation helpful? Give feedback.
-
That sounds like a bug in the code then. If the API documentation clearly says that a 204 No Content response is one of the potential responses, then the code ought to be able to handle a request that doesn't have an Accept header (because the client isn't interested in accepting any response body!) Insisting upon an Accept header to send a 204 No Content response is just wrong.
This is a case where the OpenAPI document and the implementing code are in conflict. I don't see how any of this is an issue with the OpenAPI specification. |
Beta Was this translation helpful? Give feedback.
-
@karenetheridge No, it's not wrong. It's perfectly valid (and it's also a common practice in Rails). Find a single standard (outside your OpenAPI spec) that says that it is invalid. The Accept header in the request simply tells the application code to return a response in JSON format instead of responding in HTML format. How is that invalid?
|
Beta Was this translation helpful? Give feedback.
-
@collimarco As said above, you cannot express in OpenAPI 3.x that
I do not see a way to add that to OpenAPI 3.x, and it seems a valid use case for signatures in OpenAPI 4.0. Please add it to the signatures discussion to make sure that this use case is not overlooked when designing OpenAPI 4.0. |
Beta Was this translation helpful? Give feedback.
-
@collimarco I moved this to a discussion as a precursor step to migrating it to OAI/sig-moonwalk but GitHub is not letting me transfer it at the moment. I'll try again later or see if some permissions have gotten messed up, but I just wanted to explain the conversion. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Describe the error in the specification
Some common web frameworks, like Laravel or Rails, decide what type of response to provide based on the Accept header of the HTTP request.
For example in Rails you may have:
If you are building an OpenAPI spec to deal with that Rails API, you need to specify this in the request:
Otherwise, without that header in the request, the default response type is HTML and you will get a HTML response (or a redirect in this case) instead of the 204.
Additional context
This issue is even more serious since you cannot set the required header explicitly, due to this sentence in the OpenAPI official specification:
Beta Was this translation helpful? Give feedback.
All reactions