Curl how to POST multipart/form-data data and How to Read multipart/form-data in flask request

自作多情 提交于 2019-12-21 20:40:48

问题


I tried posting multipart/form-data through CURL which contains,

  • A JSON Object
  • Stream object pdf and jpeg file
  curl -i -X POST -H  
"Authorization":"eyJhbGciOiJIUzI1NiIsImV4cCI6MTQyNjcwNTY4NiwiaWF0IjoxNDI2NzAyMDg2fQ.eyJpZCI6MTc3fQ.yBwLFez2RnxTojLniL8YLItWVvBb90HF_yfhcsyg3lY" -H    

"Content-Type:application/multipart/form-data" -d '{"user data":  {"preferred_city":"Newyork","within_radious":"5"}}' --data-binary   

"uploaded_documents":@mydocument.pdf http://127.0.0.1:5000/api/city

Now, i need to read this multipart data in flask request object.i tried

 request.data

It did print the data but i am not sure how to read the stream object and store the file to disk.


回答1:


There are a few problems with your curl command, all of which might contribute to the problem:

  1. application/multipart/form-data is not a valid MIME type, so the Content-Type is invalid. For file uploads the content type would usually be multipart/form-data. Also, you don't need to specify the content type, curl will work it out based on the arguments.
  2. Using -F instead of -d will cause curl to generate and post a multipart/form-data form with a valid boundary.
  3. A name should be specified for each form field.

Putting that together results in this curl command:

curl -i -H "Authorization":"eyJhbGciOiJIUzI1NiIsImV4cCI6MTQyNjcwNTY4NiwiaWF0IjoxNDI2NzAyMDg2fQ.eyJpZCI6MTc3fQ.yBwLFez2RnxTojLniL8YLItWVvBb90HF_yfhcsyg3lY" \
    -F user_data='{"user data": {"preferred_city":"Newyork","within_radious":"5"}}' \
    -F uploaded_documents=@mydocument.pdf \
    http://127.0.0.1:5000/api/city

You can specify the content type for each part if you don't like the ones selected by curl (the file will be application/octet-stream):

curl -i -H "Authorization":"eyJhbGciOiJIUzI1NiIsImV4cCI6MTQyNjcwNTY4NiwiaWF0IjoxNDI2NzAyMDg2fQ.eyJpZCI6MTc3fQ.yBwLFez2RnxTojLniL8YLItWVvBb90HF_yfhcsyg3lY" \
    -F 'user_data={"user data": {"preferred_city":"Newyork","within_radious":"5"}};type=application/json' \
    -F 'uploaded_documents=@mydocument.pdf;type=application/pdf' \
    http://127.0.0.1:5000/api/city

The last command would generate a HTTP request like this:

POST /api/city HTTP/1.1
User-Agent: curl/7.32.0
Host: 127.0.0.1:5000
Accept: */*
Authorization:eyJhbGciOiJIUzI1NiIsImV4cCI6MTQyNjcwNTY4NiwiaWF0IjoxNDI2NzAyMDg2fQ.eyJpZCI6MTc3fQ.yBwLFez2RnxTojLniL8YLItWVvBb90HF_yfhcsyg3lY
Content-Length: 496
Expect: 100-continue
Content-Type: multipart/form-data; boundary=------------------------1ab997efff76fe66

--------------------------1ab997efff76fe66
Content-Disposition: form-data; name="user_data"
Content-Type: application/json

{"user data": {"preferred_city":"Newyork","within_radious":"5"}}
--------------------------1ab997efff76fe66
Content-Disposition: form-data; name="uploaded_documents"; filename="mydocument.pdf"
Content-Type: application/pdf

this is the mydocument.pdf file.
it should be a pdf file, but this is easier to test with.

--------------------------1ab997efff76fe66--

Then in Flask you can access the form data using request.form, e.g. request.form['user_data']. As it is a json string, you could load it using json.loads(request.form['user_data']).

The uploaded file can be accessed by using request.file as described here and here in the Flask documentation.



来源:https://stackoverflow.com/questions/29231926/curl-how-to-post-multipart-form-data-data-and-how-to-read-multipart-form-data-in

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!