In my request, I have an array with string values and numbers. Here is a simple example:
import requests
from requests_toolbelt.multipart.encoder import MultipartEncoder
m = MultipartEncoder(
fields={'field0': 'value', 'field1': ['Test1', 'test2'],
'field2': ('filename', 'test', 'text/plain')})
r = requests.post('http://httpbin.org/post', data=m, headers={'Content-Type': m.content_type})
print(r.text)
It woks but reqest is invalid. Here is response:
{
"args": {},
"data": "",
"files": {
"field1": "test2",
"field2": "test"
},
"form": {
"field0": "value"
},
"headers": {
"Accept": "*/*",
"Accept-Encoding": "gzip, deflate",
"Content-Length": "378",
"Content-Type": "multipart/form-data; boundary=99e888bfc6254e4b8a428f9f234a7be0",
"Host": "httpbin.org",
"User-Agent": "python-requests/2.22.0",
"X-Bluecoat-Via": "85fb7b65b90c79fc"
},
"json": null,
"url": "https://httpbin.org/post"
}
I am unsing:
Here is an example of how to fix it.
import requests
from requests_toolbelt.multipart.encoder import MultipartEncoder
m = MultipartEncoder(
fields=[
('field0', 'value'), ('field1', 'Test1'), ('field1', 'Test2'),
('field2', ('filename', 'test', 'text/plain'))
]
)
r = requests.post('http://httpbin.org/post', data=m, headers={'Content-Type': m.content_type})
print(r.text)
Result:
{
"args": {},
"data": "",
"files": {
"field2": "test"
},
"form": {
"field0": "value",
"field1": [
"Test1",
"Test2"
]
},
"headers": {
"Accept": "*/*",
"Accept-Encoding": "gzip, deflate",
"Content-Length": "452",
"Content-Type": "multipart/form-data; boundary=3034efa776d04f4cb83f899783386e3a",
"Host": "httpbin.org",
"User-Agent": "python-requests/2.22.0",
"X-Bluecoat-Via": "9ca77bf6f5d34bc6"
},
"json": null,
"url": "https://httpbin.org/post"
}