请求多部分数据

时间:2019-06-07 11:08:34

标签: python request python-requests

我不知道我是否发送了正确的多部分请求。我使用requests_toolbelt,但是当我查看request.prepare()进行调试时,发现我的请求只是一团糟。

记录器的输出

DEBUG: <MultipartEncoder: 
{('{"content-disposition": "form-data; name=\\"data\\"", "content-type": "text/plain;charset = \\"UTF-8\\"", "content-transfer-encoding": "8bit"}', '{"session_id": "000001", "device_id": 1, "event_datetime": "2019-06-07T13:41:32.980755", "card_num": "1030203", "lpr_num": ""}'), 
('{"content-disposition": "form-data; name=\\"do\\"", "content-type": "text/plain;charset = \\"UTF-8\\"", "content-transfer-encoding": "quoted - printable"}', 'open_session'), 
('{"content-disposition": "form-data; name=\\"crc\\"", "content-type": "text/plain; charset=\\"UTF-8\\"", "content-transfer-encoding": "quoted-printable"}', 'my_crc'), 
('{"content-disposition": "form-data; name=\\"apikey\\"", "content-type": "text/plain;charset = \\"UTF-8\\"", "content-transfer-encoding": "quoted-printable"}', 'secret')}>

我手动设置了标头,以防止端点方面出现任何问题。 我

self.headers = {"content-type": "multipart/form-data;"+self.boundary}
multipart_data = MultipartEncoder(boundary=self.boundary,fields={(self.do_header, self.do),(self.data_header, self.data),(self.apikey_header, sf_key),(self.crc_header, self.crc)})
response = requests.Request('POST',sf_endpoint,headers=self.headers,data = multipart_data)

prepared = response.prepare()

这是说明中所述的内容:

POST / HTTP/1.0
Content-Type: multipart/form-data; boundary=--------052119221032743
Content-Length: 877
Host: localhost:7070
User-Agent: Mozilla/3.0

----------052119221032743
Content-Disposition: form-data; name="do"
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

do_data
----------052119221032743
Content-Disposition: form-data; name="data"
Content-Type: application/json; charset="UTF-8"
Content-Transfer-Encoding: 8bit

{}
----------052119221032743
Content-Disposition: form-data; name="apikey"
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

api_key_value
----------052119221032743
Content-Disposition: form-data; name="crc"
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

crc_hex_data
----------052119221032743--

0 个答案:

没有答案