Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Removing a header before proxying the request #44

Unanswered
wabiloo asked this question in Q&A
Discussion options

Hi,
I'm trying to build a FastAPI app with a reverse proxy. It sends the requests to a remote server, then modifies the response. It mostly works. However when it sits behind Cloudfront and makes a request to another Cloudfront endpoint (CF1 -> FastAPI -> CF2), I get 403 errors. It seems that it is linked to having a "via" header.
I'm trying to remove that header before sending the request, but when I do that, I get an Exception in ASGI application.

Here is the relevant part of the code:

proxy = ReverseHttpProxy(AsyncClient(), base_url=f"http://{fqdn}/")
@app.api_route("/{path:path}", methods=["GET", "HEAD", "OPTIONS"])
async def _(request: Request, path: str = ""):
 
 # Remove the "via" header. 
 if "Via" in request.headers:
 request.headers = MutableHeaders(dict(request.headers.items()))
 del request.headers["Via"]
 proxy_response = await proxy.proxy(request=request, path=path)
 query_params = dict(request.query_params)
 return proxy_response

If I comment the if statement, no issue (other than the 403 obviously).

Any idea what I'm doing wrong?

You must be logged in to vote

Replies: 1 comment 2 replies

Comment options

Hi @wabiloo

I get an Exception in ASGI application

Can you send me the complete exception traceback?

You must be logged in to vote
2 replies
Comment options

Hi, not sure how to get that. I couldn't figure it out.

I'll try and give you a piece of code that you can run and that reproduces it

Comment options

Hi, not sure how to get that. I couldn't figure it out.

just send me the terminal output, it might look something like this:

PS E:\GitHub\fastapi-proxy> python 
Python 3.10.8 (tags/v3.10.8:aaaf517, Oct 11 2022, 16:50:30) [MSC v.1933 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> a
Traceback (most recent call last): 
 File "<stdin>", line 1, in <module>
NameError: name 'a' is not defined 

I'll try and give you a piece of code that you can run and that reproduces it

it's best to be a minimal reproducible example

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants

AltStyle によって変換されたページ (->オリジナル) /