我想发送包含图像的python请求的multipart / form-data。
我已经尝试过一些方法,但是它不能正常工作,可以给我一些建议吗?
import requests
from requests_toolbelt.multipart.encoder import MultipartEncoder
print "Hello"
url = "https://api.testdomain.com/api/3.1/listings/"
token = 'my-token'
headers = {
'authorization': "Bearer " + token,
'Content-Type': 'multipart/form-data; boundary=mybound',
'platform': 'ios'
}
multipart_data = MultipartEncoder(
fields={
# a file upload field
'photo_0': ('pic.jpg', open('pic.jpg', 'rb'), 'image/jpeg','image_0.jpg'),
# plain text fields
'shipping_sg_other_name': 'atikrahman',
'collection_id': '2239',
'description': 'Test bag descriptions 5',
'material_type': '',
'location_latlon': '999.0,999.0',
'photo_0_hash': '',
'multi_quantities': 'true',
'shipping_sg_normal_options': 'false',
'price': '60',
'abcpay': 'false',
'abcpay_sg_choices': 'atikrahman',
'condition': '2',
'shipping_sg_other_fee': '3.0',
'meetup': 'false',
'shipping_sg_smartpac_options': 'false',
'is_mobile_verification_listing_flow_enabled': 'true',
'shipping_sg_other': 'true',
'title': 'TES bag 5',
'shipping_sg_other_details': '',
'detect_phone_number': 'false',
'brand': '',
'mailing': 'true',
'shipping_sg_registered_options': 'false',
}
)
response = requests.request("POST", url,data=multipart_data,headers=headers)
print(response.text)
我想对此http请求进行python请求。我是python新手,这就是为什么有问题。
POST /api/3.1/listings/ HTTP/1.1
Host: api.testdomain.com
Authorization: Bearer my-token
X-Client-Version: 2.103.0
Accept: */*
Accept-Language: en-CN;q=1.0, zh-Hans-CN;q=0.9
Accept-Encoding: gzip, deflate
platform: ios
Content-Length: 1913
User-Agent: Mozilla/5.0 (iPhone; CPU iPhone OS 12_0_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/16A404
build-no: 958
Connection: close
Content-Type: multipart/form-data; boundary=mybound
Cookie: __cfduid=dd4186742eb52fc594d39235a3870e1c11552535680
--mybound
Content-Type: image/jpeg
Content-Disposition: form-data; name="photo_0"; filename="image_0.jpg"
[image_source_code]
--mybound
Content-Disposition: form-data; name="shipping_sg_other_name"
atikrahman
--mybound
Content-Disposition: form-data; name="collection_id"
2239
--mybound
Content-Disposition: form-data; name="description"
Test bag descriptions 6
--mybound
Content-Disposition: form-data; name="material_type"
--mybound
Content-Disposition: form-data; name="location_latlon"
999.0,999.0
--mybound
Content-Disposition: form-data; name="photo_0_hash"
--mybound
Content-Disposition: form-data; name="multi_quantities"
true
--mybound
Content-Disposition: form-data; name="shipping_sg_normal_options"
false
--mybound
Content-Disposition: form-data; name="price"
32.0
--mybound
Content-Disposition: form-data; name="abcpay"
false
--mybound
Content-Disposition: form-data; name="abcpay_sg_choices"
Wildfemale
--mybound
Content-Disposition: form-data; name="condition"
2
--mybound
Content-Disposition: form-data; name="shipping_sg_other_fee"
3.0
--mybound
Content-Disposition: form-data; name="meetup"
false
--mybound
Content-Disposition: form-data; name="shipping_sg_smartpac_options"
false
--mybound
Content-Disposition: form-data; name="is_mobile_verification_listing_flow_enabled"
true
--mybound
Content-Disposition: form-data; name="shipping_sg_other"
true
--mybound
Content-Disposition: form-data; name="title"
Test bag 7
--mybound
Content-Disposition: form-data; name="shipping_sg_other_details"
--mybound
Content-Disposition: form-data; name="detect_phone_number"
false
--mybound
Content-Disposition: form-data; name="brand"
--mybound
Content-Disposition: form-data; name="mailing"
true
--mybound
Content-Disposition: form-data; name="shipping_sg_registered_options"
false
--mybound--
谢谢
答案 0 :(得分:1)
您可能不需要太多。
将数据和文件作为参数传递。多部分请求将自动构建。
请参见docs。
import requests
# ...
data = {
'shipping_sg_other_name': 'atikrahman',
# ....
'shipping_sg_registered_options': 'false',
}
files = ['/path/to/pic.jpg']
response = requests.post(url, data=data, files=files, headers=headers)
print(response.text)