Would be nice to be able to route request using header's Accept field (or generic header's field)
See original GitHub issueFirst Check
- I added a very descriptive title to this issue.
- I used the GitHub search to find a similar issue and didn’t find it.
- I searched the FastAPI documentation, with the integrated search.
- I already searched in Google “How to X in FastAPI” and didn’t find any information.
- I already read and followed all the tutorial in the docs and didn’t find an answer.
- I already checked if it is not related to FastAPI but to Pydantic.
- I already checked if it is not related to FastAPI but to Swagger UI.
- I already checked if it is not related to FastAPI but to ReDoc.
Commit to Help
- I commit to help with one of those options 👆
Example Code
N.A.
Description
In some cases would be nice to specify header’s field as routing rules.
One important example is to support API versioning based on Header’s Accept field
Wanted Solution
Ability to specify some header’s fields in the .get()
, .post()
… decorators
Wanted Code
from fastapi import FastAPI
app = FastAPI()
@app.get("/", accept="application/json;version=1.0")
async def root():
return {"message": "Hello World v1.0"}
@app.get("/", accept="application/json;version=1.1")
async def root():
return {"message": "Hello World v1.1"}
Alternatives
from fastapi import FastAPI
app = FastAPI()
@app.get(“/”, headers={“accept”: “application/json;version=1.0”}) async def root(): return {“message”: “Hello World v1.0”}
@app.get(“/”, headers={“accept”: “application/json;version=1.1”}) async def root(): return {“message”: “Hello World v1.1”}
Operating System
macOS
Operating System Details
No response
FastAPI Version
python -c “import fastapi; print(fastapi.version)”
Python Version
Python 3.9.7
Additional Context
No response
Issue Analytics
- State:
- Created 2 years ago
- Reactions:6
- Comments:19 (9 by maintainers)
Top Results From Across the Web
HTTP/1.1: Header Field Definitions
The Accept request-header field can be used to specify certain media types which are acceptable for the response. Accept headers can be used...
Read more >HTTP headers | Accept - GeeksforGeeks
The HTTP Accept header is a request type header. The Accept header is used to inform the server by the client that which...
Read more >HTTP headers - MDN Web Docs - Mozilla
HTTP headers let the client and the server pass additional information with an HTTP request or response. An HTTP header consists of its ......
Read more >Session Initiation Protocol (SIP) Parameters
When present in a Require or Proxy-Require header field of a ... a User Agent ability of accepting a REFER request without establishing...
Read more >HTTP header manipulation - Envoy Proxy
Envoy will always set the :scheme header while processing a request. It should always be available to filters, and should be forwarded upstream...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
You could use the specification for vendor types in the Accept header, and have headers like:
Accept: application/vnd.myapp.v1.1+json
Then you need to do 3 things:
matches
function to find the matching version(basically what @dmontagu indicates here: https://github.com/tiangolo/fastapi/issues/200#issuecomment-525126712)
Middleware looks like this
Router + Route subclasses:
Then you can plug everything together:
Haven’t tested that yet, I’ll take a look. I was thinking that since it’s using the accept header for versioning, we could use the media type option of the open API spec: https://swagger.io/docs/specification/media-types/. That could work, at least for the response format.
Regarding request params, I don’t know exactly how that would be described in swagger.