I am using Amazon API Gateway that forwards requests to a FastAPI server (I am not using nginx). I am trying to get the user's IP address in a FastAPI endpoint, but it does not seem to be working (x_forwarded_for
is empty).
Here is a snippet of my FastAPI backend:
@app.post("/upload")
async def runUpload(file: UploadFile,request: Request, x_forwarded_for: str = Header(None)):
userIp = x_forwarded_for.split(',')[0].strip() if x_forwarded_for else request.client.host
This is how I start my server:
if __name__ == '__main__':
uvicorn.run(app,port=PORT,host='0.0.0.0',proxy_headers=True,forwarded_allow_ips="*")
when i print the request.headers i see (of course with correct numbers), so the data is here...
'forwarded': 'by=1.2.3.4;for=5.6.7.8;host=555.execute-api.us-east-1.amazonaws.com;proto=https'