Not getting user user given value for "Accept" header from swagger ui.
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
from fastapi import FastAPI, Header
from typing import Optional
import cv2
import io
import numpy as np
from fastapi.responses import StreamingResponse, JSONResponse
app = FastAPI()
@app.get("/image")
def get_image(accept: Optional[str] = Header("image/png")):
accept_headers = accept.split(",")
dummy = np.zeros((512, 512))
if "image/png" in accept_headers or "*/*" in accept_headers:
return StreamingResponse(
io.BytesIO(cv2.imencode(".png", dummy)[1]), media_type="image/png"
)
elif "image/jpeg" in accept_headers:
return StreamingResponse(
io.BytesIO(cv2.imencode(".jpeg", dummy)[1]), media_type="image/jpeg"
)
else:
return JSONResponse(
status_code=406,
content={
"message": "encoding of type {} is not supported".format(accept)
},
)
Description
Hi, I have an API that is sending responses in different encoding depending on the user and to detect which encoding server should use I am using the “Accept” header. But when I tried to test it using Swagger UI, it always sent “application/json”.
Operating System
Linux, Windows
Operating System Details
No response
FastAPI Version
0.70.1
Python Version
Python 3.9.5
Additional Context
No response
Issue Analytics
- State:
- Created 2 years ago
- Reactions:1
- Comments:8
Top Results From Across the Web
Swagger UI Not adding Header to Requests - Stack Overflow
I am using Swagger 2.0 and Swagger UI 3.0. 3. This adds an Authorize button the the Swagger UI where the user can...
Read more >Describing Responses - Swagger
An API specification needs to specify the responses for all API operations. Each operation must have at least one response defined, usually a...
Read more >API Keys - Swagger
Some APIs use API keys for authorization. ... or as a request header: ... (not to be confused with the API key name,...
Read more >Cookie Authentication - Swagger
Note for Swagger UI and Swagger Editor users: Cookie authentication is currently not supported for "try it out" requests due to browser security ......
Read more >Describing Parameters - Swagger
paths: /users/{userId}:; get: summary: Get a user by ID; parameters: ... Note: Header parameters named Accept , Content-Type and Authorization are not ......
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
i already knew this api work from source other than swagger, but it would have been nice if it also worked on swagger.
Observations:
In this code,
accept
is overwritten by openapi docs as you observed:BUT if in terminal you type
It works just fine for me.
If then we change
accept
tocustom_accept
:You get working
custom_accept
header just as expected and docs are not broken.Conclusions:
It’s not FastAPI related, not in any direct way I mean. I found something that may be interesting for you: https://github.com/swagger-api/swagger-ui/issues/7374
That’s kind of expected for me that your
accept
header will be overwritten by default one.So, with any luck, you have 3 options: