4

Is it possible to send a multipart/form-data with python requests without sending a file?

My requests header should look like this:

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

content
--3eeaadbfda0441b8be821bbed2962e4d--

But actually it looks like this:

--3eeaadbfda0441b8be821bbed2962e4d
Content-Disposition: form-data; name="file"; filename="file"
Content-Type: text/plain

content
--3eeaadbfda0441b8be821bbed2962e4d--

I'm using this function:

response = requests.post('url', data, files=('file', 'file'))

1 Answers1

4

Yes, you could, just pass dictionary with tuples as values to post method. When you specify files parameter to this method requests add multipart/form-data header. But you free to pass dictionary with tuples as files:

url = 'http://httpbin.org/post' 
files = {'file': (None, 'some,data,to,send\nanother,row,to,send\n')}
r = requests.post(url, files=files) 

http://docs.python-requests.org/en/latest/user/quickstart/#post-a-multipart-encoded-file

Eugene Soldatov
  • 9,755
  • 2
  • 35
  • 43
  • 1
    It works, but I don't need the filename="hello" part in the header of the request. Is there a simple way to remove it? –  Dec 24 '15 at 11:09
  • what are you talking about? Please show me your headers. – Eugene Soldatov Dec 24 '15 at 11:12
  • The header I want to get is the first code I posted. What I'm actually getting is the second code I posted. In the request I don't need the filename="..." part. Is there a simple way to remove that string from the request? –  Dec 24 '15 at 11:18
  • I understand now, you can pass None as filename: `files = {'file': (None, 'some,data,to,send\nanother,row,to,send\n')}` Than header will be without `filename`. – Eugene Soldatov Dec 24 '15 at 11:40