1

I would like to know if there is a way for FastAPI to receive a URL of a file as a parameter and save this file to disk? I know it is possible with the requests library using requests.get() method, but is it possible with FastAPI to receive the URL and save it directly?

I tried using file: UploadFile = File(...), but then it doesn't download the file when the URL is sent.

Chris
  • 18,724
  • 6
  • 46
  • 80
Ordnael
  • 31
  • 1
  • 6
  • Could you specify what you would like to accomplish in more detail, maybe include a pseudocode example? It's a bit difficult to understand what you are asking for. – M.O. Jan 02 '23 at 20:10
  • 1
    No, you need to pass `bytes` or `UploadFile`. – ScriptKiddieOnAComputer Jan 03 '23 at 06:21
  • 1
    Please take a look at related answers [here](https://stackoverflow.com/a/74239367/17865804), as well as [here](https://stackoverflow.com/a/71398460/17865804). – Chris Jan 04 '23 at 09:46
  • Good, I applied it here with httpx... which is recommended by fastapi. thanks – Ordnael Jan 09 '23 at 11:42

1 Answers1

1

I don't believe so. I've come across this before and was unable to find a solution (and ended up using requests like you mentioned), but seeing this I wanted to check again more thoroughly.

Reviewing the uvicorn and fastapi repositories by searching the code itself, I see no functions/code that reference requests or urllib (they do use urllib.parse/quote, etc though) that would be 2 likely suspects to build requests. They do use httpx.AsyncClient, but only in tests. I would expect to see some use of these libraries in the main uvicorn/fastapi libraries if they had code to make external requests.

Seeing the above, I actually think I will change my code to use httpx.AsyncClient anyways since it is already a dependency.

Adam Huganir
  • 11
  • 1
  • 1