Reputation: 13
Is there any way to send each different 'Content-Type' in the multipart-form data for each single input param
e.g.
Content-Type: multipart/form-data;boundary=q235Ht2tTWJhuFmC8sJxbQ7YGU7FwQafcZd8B
Accept-Charset: utf-8
"Content-Disposition: form-data; name="creative_id"
"Content-Type: text/plain;charset=ISO-8859-1”
…
"Content-Disposition: form-data; name=“file_role""
"Content-Type: text/plain;charset=ISO-8859-1”
…
"Content-Disposition: form-data; name="Filename""
"Content-Type: text/plain;charset=ISO-8859-1
"Content-Disposition: form-data; name="file"; filename="advertise_A.png"
"Content-Type: image/x-png"
For the whole request, the header will be Content-Type: multipart/form-data; but for its params like creative_id and file_role, I would like to send Content-Type: text/plain;charset=ISO-8859-1 and for the file itself Content-Type: image/x-png
I tried this in two ways, but it doesnt work:
headers = {'Content-Type':'text/plain;charset=ISO-8859-1'}
files = {'file': open(asset_file, 'rb')}
and then in POST (url, files=files, headers=headers, params=values)
OR
files = {'file1': (open(asset_file, 'rb'), 'image/x-png'), 'creative_id': (1727968, 'text/plain;charset=ISO-8859-1'), 'file_role': ('PRIMARY', 'text/plain;charset=ISO-8859-1')}
and then in POST (url, files=files)
Upvotes: 1
Views: 703
Reputation: 15518
You're really close with the second example. If you provide a dict with tuples as its value, the tuples have this form:
(filename, file object or content, [content type], [headers])
where the content type
and headers
fields are optional.
This means you want to do this:
files = {'file': ('advertise_A.png', open(asset_file, 'rb'), 'image/x-png'), 'creative_id': ('', '1727968', 'text/plain;charset=ISO-8859-1'), 'file_role': ('', 'PRIMARY', 'text/plain;charset=ISO-8859-1'), 'Filename': ('', 'advertise_A.png', 'text/plain;charset=ISO-8859-1')}
r = requests.post(url, files=files)
Doing the above with a file that contains only the string basic_test
, I get the following result:
Content-Type: multipart/form-data; boundary=82c444831d6a450ba5c4ced2e1cc7866
--82c444831d6a450ba5c4ced2e1cc7866
Content-Disposition: form-data; name="creative_id"
Content-Type: text/plain;charset=ISO-8859-1
1727968
--82c444831d6a450ba5c4ced2e1cc7866
Content-Disposition: form-data; name="file_role"
Content-Type: text/plain;charset=ISO-8859-1
PRIMARY
--82c444831d6a450ba5c4ced2e1cc7866
Content-Disposition: form-data; name="file"; filename="advertise_A.png"
Content-Type: image/x-png
basic_test
--82c444831d6a450ba5c4ced2e1cc7866
Content-Disposition: form-data; name="Filename"
Content-Type: text/plain;charset=ISO-8859-1
advertise_A.png
--82c444831d6a450ba5c4ced2e1cc7866--
Upvotes: 2