Replies: 1 comment
-
It's likely because the session cookies are leading you back to the login page instead of letting you access the file directly.
Try something like this: from requests.auth import HTTPBasicAuth
import requests
@app.post("/upload-file/")
def upload_file(request: UploadFileRequest):
local_filename = f"{MEDIA_DIR}/{request.fileName}"
url = request.fileURL
print(f"Downloading file from {url}")
username = '[email protected]'
password = 'test1234'
# Make the request without cookies
response = requests.get(url, auth=HTTPBasicAuth(username, password), cookies={})
if response.status_code == 200:
with open(local_filename, 'wb') as file:
file.write(response.content)
print("File successfully downloaded")
else:
print(f"Error: {response.status_code}") |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
First Check
Commit to Help
Example Code
Description
I have a FastAPI backend running locally on port 8000. I also have Liferay running locally via Docker. Files can be uploaded via Liferay. The upload triggers an event that sends the URL of the file to my FastAPI backend to move the file there.
I first tried using a standalone Python script to make a GET request to the URL to get the file, which worked fine. The content of the file is complete and correct.
I then implemented the same code as a function in my FastAPI endpoint (which is called by Liferay). The file is downloaded, but the content is completely different. The content is an HTML page. When I opened the HTML document I saw that I landed on the login page of Liferay.
The only difference between the two methods is the request header.
For the standalone script it looks like this:
{'User-Agent': 'python-requests/2.31.0', 'Accept-Encoding': 'gzip, deflate', 'Accept': '/', 'Connection': 'keep-alive'}
For the endpoint it looks like this:
{'User-Agent': 'python-requests/2.31.0', 'Accept-Encoding': 'gzip, deflate', 'Accept': '/', 'Connection': 'keep-alive', 'Cookie': 'JSESSIONID=FDFC0EB495BE73045A5383EFAD88E0B4; COOKIE_SUPPORT=true; GUEST_LANGUAGE_ID=en_US'}
Is it possible to omit the cookies in a request from FastAPI? Or does anyone else know the reason for this?
Operating System
Windows
Operating System Details
No response
FastAPI Version
0.110.2
Pydantic Version
2.7.1
Python Version
Python 3.10.13
Additional Context
No response
Beta Was this translation helpful? Give feedback.
All reactions