问题描述
我尝试通过CURL发布multipart / form-data,其中包含
$ b
卷曲-i -X POST -H
的授权: eyJhbGciOiJIUzI1NiIsImV4cCI6MTQyNjcwNTY4NiwiaWF0IjoxNDI2NzAyMDg2fQ.eyJpZCI6MTc3fQ.yBwLFez2RnxTojLniL8YLItWVvBb90HF_yfhcsyg3lY -H
内容类型:应用/多部分/格式数据-d{user data:{preferred_city:Newyork,within_radious:5}}'--data-binary
uploaded_documents:@ mydocument.pdf http://127.0.0.1:5000/api/city
现在,我需要阅读这个多部分在请求对象的数据。我试过了
$ $ $ $ $ $ $ $ request.data
code $
它确实打印了数据,但是我不确定如何读取流对象并将文件存储到磁盘。
Content-Type
无效。对于文件上传,内容类型通常是 multipart / form-data
。另外,你不需要指定内容类型,curl会根据参数来处理它。 -F
而不是 -d
会导致curl生成和发布 multipart / form-data
表单结果在该卷曲的命令:
卷曲-i -H 授权: 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
您可以指定内容类型对于每个部分,如果你不喜欢curl选择的文件(该文件将是应用程序/八位字节流):
卷曲-i -H 授权: eyJhbGciOiJIUzI1NiIsImV4cCI6MTQyNjcwNTY4NiwiaWF0IjoxNDI2NzAyMDg2fQ.eyJpZCI6MTc3fQ.yBwLFez2RnxTojLniL8YLItWVvBb90HF_yfhcsyg3lY \
-F的user_data = { 用户数据:{ preferred_city: 纽约, within_radious: 5}};类型= application / json'\
-F'uploaded_documents=@mydocument.pdf; type = application / pdf'\
http://127.0.0.1:5000/api/city
最后一个命令会生成一个HTTP请求,如下所示:
POST / api / city HTTP / 1.1
User-Agent:curl / 7.32.0
Host:127.0.0.1:5000
Accept:* / *
授权:eyJhbGciOiJIUzI1NiIsImV4cCI6MTQyNjcwNTY4NiwiaWF0IjoxNDI2NzAyMDg2fQ.eyJpZCI6MTc3fQ.yBwLFez2RnxTojLniL8YLItWVvBb90HF_yfhcsyg3lY
的Content-Length:496
期望值:100继续
内容类型:多部分/格式的数据; 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
这是mydocument.pdf文件。
它应该是一个pdf文件,但这是更容易测试。
-------------------------- 1ab997efff76fe66--
然后在Flask中,你可以使用 request.form
来访问表单数据,例如的request.form [的user_data]
。因为它是一个json字符串,所以你可以使用 json.loads(request.form ['user_data'])
。
使用 request.file
可以访问上传的文件和在Flask文档中。
I tried posting multipart/form-data through CURL which contains,
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.
There are a few problems with your curl command, all of which might contribute to the problem:
application/multipart/form-data
is not a valid MIME type, so theContent-Type
is 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
-F
instead of-d
will cause curl to generate and post amultipart/form-data
form 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.
这篇关于curl如何POST多部分/表单数据数据以及如何读取多部分/表单数据的请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!