Question: Response headers needs to be set/assigned again
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.
- After submitting this, I commit to one of:
- Read open issues with questions until I find 2 issues where I can help someone and add a comment to help there.
- I already hit the “watch” button in this repository to receive notifications and I commit to help at least 2 people that ask questions in the future.
- Implement a Pull Request for a confirmed bug.
Example
So, the following code is simple. Sets headers and done! BUT it’s not how it looks like.
from fastapi import FastAPI, Response
from fastapi.responses import RedirectResponse, FileResponse
from os.path import isfile
app = FastAPI()
@app.get("/")
def read_root(resp: Response):
resp.headers['foo'] = 'bar'
return RedirectResponse('https://printer.discord.com')
# OR the FileResponse
@app.get("/baz/{location:path}")
def read_baz(resp: Response):
if isfile(location):
return FileResponse(location)
Description
- Open the browser and call the endpoint
/
. - It returns a 307 Temporary Redirect to bla bla… BUT WITHOUT THE HEADERS
- Well, the fix is simple.
RedirectResponse(..., headers=resp.headers)
BUT it actually took me a while to think of it and it would be awesome to see objects withinfastapi.responses
actually respectfastapi.Response
.
Environment
- OS: macOS
- FastAPI Version: 0.65.0
- Python version: Python 3.9.5
Issue Analytics
- State:
- Created 2 years ago
- Reactions:1
- Comments:5 (2 by maintainers)
Top Results From Across the Web
Access-Control-Allow-Headers is set but impossible to get ...
I need to get some information from the custom response header. On the server side, this custom response header was added in the ......
Read more >How to troubleshoot the error "Failed to process response ...
This error almost always means that the policy did not receive any data. If you increase the logging from error to debug, you...
Read more >Retry-After - HTTP - MDN Web Docs
The Retry-After response HTTP header indicates how long the user agent should wait before making a follow-up request.
Read more >Understanding response headers policies - Amazon CloudFront
You can use a response headers policy to specify the HTTP headers that Amazon CloudFront adds to responses that it sends to viewers....
Read more >Http Client: How to get a specific Response header field value ...
Hi, I have a requirements to integrate with KOHA http://wiki.koha-community.org/wiki/Koha_/svc/_HTTP_API (see for API details).
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
Makes sense, another object is another object, and objects acting polymorphic could be a nightmare while debugging.
You’re mutating the headers in a different object and not returning that object. I’d (and probably most others) prefer this behavior over what you’re suggesting.