【发布时间】:2019-12-13 12:22:28
【问题描述】:
我正在尝试发送 2 张照片 + json-payload 请求。首先,我按照How to send a "multipart/form-data" with requests in python? 中的描述做了,但它不起作用。
from requests_toolbelt.multipart.encoder import MultipartEncoder
import requests
mp_encoder = MultipartEncoder(fields={"parameters": {"sysId": 1, "clientId": 4029487, "fsid": 'ChNKoXqa87YucQ1nlf3hJGTl',
"deviceId":"89DEB49F37DD49559D124C9F3AFA2A54"},
'file1':('img317.jpg',open('/Users/12fhntv21Q/Downloads/pasport_rf.jpg',"rb"),'image/jpeg'),
'file2':('img317.jpg',open('/Users/12fhntv21Q/Downloads/pasport_rf_1.jpg',"rb"),'image/jpeg')
}
)
print(mp_encoder)
url = 'https://clientsapi01./..../'
response = requests.post(url,headers={'Content-Type': mp_encoder.content_type}, data=mp_encoder)
给出这样的错误:
Traceback (most recent call last):
File "/Users/12fhntv21Q/PycharmProjects/Api_Test/Client_CUPIS/identification_cupis.py", line 17, in <module>
'file2':('img317.jpg',open('/Users/12fhntv21Q/Downloads/pasport_rf_1.jpg',"rb"),'image/jpeg')
File "/Users/12fhntv21Q/Api_Test/lib/python3.7/site-packages/requests_toolbelt/multipart/encoder.py", line 125, in __init__
self._prepare_parts()
File "/Users/12fhntv21Q/Api_Test/lib/python3.7/site-packages/requests_toolbelt/multipart/encoder.py", line 246, in _prepare_parts
self.parts = [Part.from_field(f, enc) for f in self._iter_fields()]
File "/Users/12fhntv21Q/Api_Test/lib/python3.7/site-packages/requests_toolbelt/multipart/encoder.py", line 246, in <listcomp>
self.parts = [Part.from_field(f, enc) for f in self._iter_fields()]
File "/Users/12fhntv21Q/Api_Test/lib/python3.7/site-packages/requests_toolbelt/multipart/encoder.py", line 494, in from_field
body = coerce_data(field.data, encoding)
File "/Users/12fhntv21Q/Api_Test/lib/python3.7/site-packages/requests_toolbelt/multipart/encoder.py", line 472, in coerce_data
return CustomBytesIO(data, encoding)
File "/Users/12fhntv21Q/Api_Test/lib/python3.7/site-packages/requests_toolbelt/multipart/encoder.py", line 535, in __init__
buffer = encode_with(buffer, encoding)
File "/Users/12fhntv21Q/Api_Test/lib/python3.7/site-packages/requests_toolbelt/multipart/encoder.py", line 416, in encode_with
return string.encode(encoding)
AttributeError: 'dict' object has no attribute 'encode'
尝试从邮递员导入请求代码(其中2张照片和参数发送正确)
导入请求
url = "https://clientsapi01/..../"
payload = "------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition:" \
" form-data; name=\"parameters\"\r\n\r\n{\"sysId\":1,\n\"lang\":\"ru\"," \
"\n\"clientId\":4029487,\n\"fsid\":\"ChNKoXqa87YucQ1nlf3hJGTl\"," \
"\n\"devPrototype\":false,\n\"devPrototypeValue\":0," \
"\n\"deviceId\":\"89DEB49F37DD49559D124C9F3AFA2A54\"}" \
"\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW\r\n" \
"Content-Disposition: form-data; name=\"file1\"; " \
"filename=\"img317.jpg\"\r\nContent-Type: image/jpeg\r\n\r\n\r\n" \
"------WebKitFormBoundary7MA4YWxkTrZu0gW\r\n" \
"Content-Disposition: form-data; name=\"file2\"; filename=\"img318.jpg" \
"\"\r\nContent-Type: image/jpeg\r\n\r\n\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW--"
headers = {
'content-type': "multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW",
'Content-Type': "multipart/mixed",
'User-Agent': "PostmanRuntime/7.18.0",
'Accept': "*/*",
'Cache-Control': "no-cache",
'Postman-Token': "9faf7642-3ba3-44da-9353-295a90073191,93b2a684-2b39-496a-b492-68899c3cb82c",
'Host': "clientsapi01.bksndbx.com",
'Accept-Encoding': "gzip, deflate",
'Content-Length': "4566826",
'Connection': "keep-alive",
'cache-control': "no-cache"
}
response = requests.request("POST", url, data=payload, headers=headers)
print(response.text)
Вut in 也不起作用。 最后,这个方法:
payload_for_request = {"parameters": [{"sysId": 1, "clientId": 4029487, "fsid": 'JXvI1jbXteO8NOpRZPAeKeeh',
"deviceId":"89DEB49F37DD49559D124C9F3AFA2A54",
"lang":"ru"}]}
files = [("file1",("img317.jpg", open('img317.jpg',"rb"),'image/jpeg')),
("file2", ("img318.jpg",open('img318.jpg',"rb"), 'image/jpeg'))]
headers={'Content-Type': 'multipart/form-data; boundary=93c1068f0d0c354feca39cdd75562cf0'}
r = requests.post(url, data=payload_for_request, files=files)
print(r.text)
print(r.headers)
同样抛出错误 - {"result":"error","errorCode":1,"errorMessage":"bad request","errorValue":"common parameters parsing error"}
【问题讨论】:
标签: python rest api request automated-tests