Skip to content Skip to sidebar Skip to footer

Python Requests - Post Multipart/form-data Without Filename In Http Request

I am trying to replicate the following POST request using the requests module in python: POST /example/asdfas HTTP/1.1 Host: example.com User-Agent: Mozilla/5.0 (Windows NT 6.1; WO

Solution 1:

The solution is to use tuples when passing parameters to the files argument:

import requests
requests.post('http://example.com/example/asdfas', files={'value_1': (None, '12345'), 'value_2': (None, '67890')})

Works as expected:

'Accept': '*/*', 
'Accept-Encoding': 'gzip, deflate, compress', 
'Content-Length': '228', 
'User-Agent': 'python-requests/2.2.1 CPython/3.3.2 Windows/7', 
'Content-Type': 'multipart/form-data; boundary=85e90a4bbb05474ca1e23dbebdd68ed9'--85e90a4bbb05474ca1e23dbebdd68ed9
Content-Disposition: form-data; name="value_1"12345--85e90a4bbb05474ca1e23dbebdd68ed9
Content-Disposition: form-data; name="value_2"67890--85e90a4bbb05474ca1e23dbebdd68ed9--

Solution 2:

import requests
from requests_toolbelt import MultipartEncoder

url = 'http://example.com/example/asdfas'
fields = {'value_1':'12345', 'value_2': '67890'}

data = MultipartEncoder(fields=fields)
headers["Content-type"] = m.content_type

requests.post(url=url, data=data, headers=headers)

Post a Comment for "Python Requests - Post Multipart/form-data Without Filename In Http Request"