Запрос данных из нескольких частей - PullRequest
0 голосов
/ 07 июня 2019

Я не могу понять, отправляю ли я правильный многочастный запрос или нет.Я использую 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--
...