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] error uploading a file with a post request

See original GitHub issue

Description

How do I upload a file with a post request? I have done what is described here - https://fastapi.tiangolo.com/tutorial/request-files/ - so the following code is in my main.py file

from fastapi import FastAPI, Request, File, UploadFile

app = FastAPI

@app.post('/uploaddata/')
async def create_upload_file(file: UploadFile = File(...)):
    return {"filename": file.filename}

and on the client side i have a simple form like this:

  <form method="post" action="/uploaddata/" enctype="multipart/form-data">
            <button class="btn btn-outline-secondary" type="submit">upload</button>
            <input type="file" id="file_upload">
   </form>

I get the error: {“detail”:“There was an error parsing the body”}, just like issue #201 https://github.com/tiangolo/fastapi/issues/201 In that case the problem seemed to be that python-multipart wasn’t installed. I have installed it, but I still get the error. Is there something obviously wrong? If not, how can I get hold of more information that just this reply: {“detail”:“There was an error parsing the body”}?

thanks!

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
retniktcommented, May 4, 2020

You are missing the name attribute on the HTML form, it must be the same as the parameter name (in your case file). So:

<form method="post" action="/uploaddata/" enctype="multipart/form-data">
    <button class="btn btn-outline-secondary" type="submit">upload</button>
    <input type="file" id="file_upload" name="file">
</form>
0reactions
pranithan-kangcommented, Jul 30, 2020

This question links to this issue.

Read more comments on GitHub >

github_iconTop Results From Across the Web

File Upload in Angular giving error when posting POST request
You are sending only file name but not actual file blob. Try to do below changes,. component.ts : uploadFile() { this.currentFile = this....
Read more >
Error 500 when uploading a file via API - Canvas Community
I figured this out finally. It appears that posting to inscloudgate requires that you include the "filename" as part of the form data...
Read more >
File Upload Question - Qualtrics
... Building a Consent Form · Displaying Messages Based on Scoring · Screen-Out Management · Survey Tips & Tricks · Starting a Survey...
Read more >
Question: How to upload a file using HTTP POST Form-Data?
I want to upload a file using HTTP POST where username and password are to be filled using form-data.
Read more >
File Upload Question - SurveyMonkey Help
File Upload questions support the following file types: PDF; DOC, DOCX; PNG; JPG, JPEG; GIF ... Customize the error message that appears when...
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