1

I am fairly a noob at this and have been trying to use requests modules to post a multipart/form-data. To clarify, the exact test case I am trying to use is the one same as in https://github.com/kennethreitz/requests/issues/1081. i.e. I am trying to do a post without a file :

--3eeaadbfda0441b8be821bbed2962e4d
Content-Disposition: form-data; name="key1"

value1
--3eeaadbfda0441b8be821bbed2962e4d

As per the discussion on the thread, I tried MultiPart form data scheme to do the following:

import requests
from requests_data_schemes import multipart_formdata as mfd
post_data = [('mouseAction', 'toggle'), ('zone' ,'10')]
post_data = mfd(post_data)
headers = {'Content-Type': 'multipart/form-data'}

req = requests.post(<url>, data=post_data, headers=headers)

However, the test server is throwing me an error saying that it cannot detect the boundary of the multipart form data.

I tried providing the boundary in the header too, but apparently its not working.

boundary = post_data[2: post_data.find('\r\n')]
headers = {'Content-Type': 'multipart/form-data; boundary={}'.format(boundary)}

Am i missing something simple?

P.S. From a bit of surfing I found a few solutions using base urllib2 but that would be my last resort as requests lets me do a lot things pretty easily.

4

1 回答 1

4

是的,这是我必须解决的错误。此时最好执行以下操作:

from requests.packages.urllib3.filepost import encode_multipart_formdata

(content, header) = encode_multipart_formdata([('key', 'value')])
r = requests.post(url, data=content, headers={'Content-Type': header})
于 2013-08-01T02:19:28.683 回答