Home > database >  Curl POST upload file test
Curl POST upload file test

Time:05-21

I have a FastAPI with the following type:

@app.post("/extract_text") 
async def create_upload_file(upload_file: UploadFile = File(...)):
    return FileResponse(path="Outputs/ocr_output.zip", filename="{}".format(main.allinall(upload_file)) "_output.zip", media_type='application/zip')

In browser and with the help of UI that I have, I am able to upoad files and download the output file (return...).

I want to test this on a linux service and I have to test this api through a curl command. I am using this command:

 curl -X 'POST' 'http://localhost/extract_text' -H 'accept: application/json' -H 'Content-Type: multipart/form-data' -F 'file=@demo2_test_imageinput.png;type=application/json'

and here is the error I receive:

{"detail":[{"loc":["body","upload_file"],"msg":"field required","type":"value_error.missing"}]}

input is a png file avaialble in the current folder and output would be a zipfile as the output of allinall function.

when I try this:

curl -i -X POST -H "Content-Type: multipart/form-data" -F "data=@demo2_test_imageinput.png" http://localhost/extract_text

I receive this error:

HTTP/1.1 422 Unprocessable Entity
Server: nginx/1.15.12
Date: Thu, 19 May 2022 20:16:44 GMT
Content-Type: application/json
Content-Length: 95
Connection: keep-alive

CodePudding user response:

This is where you are getting it wrong.

file=@demo2_test_imageinput.png

As per your function def, your multipart field name should be upload_file

async def create_upload_file(upload_file: UploadFile = File(...))

So your curl request must have the parameter

upload_file=@demo2_test_imageinput.png

  • Related