问题
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:
application/multipart/form-datais not a valid MIME type, so theContent-Typeis invalid. For file uploads the content type would usually bemultipart/form-data. Also, you don't need to specify the content type, curl will work it out based on the arguments.- Using
-Finstead of-dwill cause curl to generate and post amultipart/form-dataform with a valid boundary. - 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