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] variable (file) field

See original GitHub issue

First check

  • 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.

Description

I’m looking for what’s the best way to have a function accept a multipart/form-data file in different configurations:

a) a specific form field like photo or movie? The file has to be uploaded as photo field then. I need to support a request like this:

requests.post(
 url='http://localhost/test',
 files=[{'photo': ('foo.png', open('foo.png', 'rb'), 'image/png')}],
)

b) be able to name the file to use, e.g. photo="attach://somefield". The request would look like:

requests.post(
  url='http://localhost/test',
  params={'photo': 'attach://random_key_banana'},
  files=[{'random_key_banana': ('foo.png', open('foo.png', 'rb'), 'image/png')}],
)

In both cases I’d like to end up with a variable photo containing the uploaded file (how doesn’t matter, I’m happy with anything like a tmp file path, a python file-like, or even a raw bytearray)

I feel like I could handle a) with

photo: UploadFile = File(...)

But how can I receive files in arbitrary fields like need for b)? The only thing I could think of is

**kwargs: Dict[str, UploadFile] = List([File(...)])

but obviously that doesn’t work.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:12 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
phy25commented, Jun 6, 2020

You can start by reading fastapi code since I guess that’s a bit low level, but you can start with https://fastapi.tiangolo.com/advanced/using-request-directly/

0reactions
github-actions[bot]commented, Jun 17, 2020

Assuming the original issue was solved, it will be automatically closed now. But feel free to add more comments or create new issues.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Setting variables (and doing other things) with questions
Within a fields question, there are many possible datatype values that you can use. These affect what the user sees and how the...
Read more >
returning field names as variables from a file - Forums - IBM
Using RPG, how can I return the field names from a physical file to be used as variables (in particular, in an SQL...
Read more >
Custom Field Creation - Qualtrics
Creating custom fields allows you to add new variables to responses after they've already been recorded. You can categorize based on items like...
Read more >
In Django, how to create file field database variable in a query?
you need to understand Django ORM first : When you do Information.objects.earliest("id") : you will get the query executed in the database.
Read more >
Using Variables to Select Field / Insert File - Claris Community
migration.user (Employee) asked a question. May 23, 2012 at 7:46 PM. Using Variables to Select Field / Insert File. Title. Using Variables to...
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