question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Question: Response headers needs to be set/assigned again

See original GitHub issue

First 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 within fastapi.responses actually respect fastapi.Response.

Environment

  • OS: macOS
  • FastAPI Version: 0.65.0
  • Python version: Python 3.9.5

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:1
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
naryal2580commented, May 12, 2021

Makes sense, another object is another object, and objects acting polymorphic could be a nightmare while debugging.

1reaction
FalseDevcommented, May 11, 2021

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found