user2528127
user2528127

Reputation:

How to send request with duplicate data in python?

i need to send request with duplicate in data. As i heard i cant send duplicates because requests use dict and i cant get duplicates in dict.

What i need to get (sniffed log from fiddler)

------WebKitFormBoundaryJm0Evrx7PZJnQkNw
Content-Disposition: form-data; name="file[]"; filename=""
Content-Type: application/octet-stream


------WebKitFormBoundaryJm0Evrx7PZJnQkNw
Content-Disposition: form-data; name="file[]"; filename="qwe.txt"
Content-Type: text/plain

example content of file qwe.txt blablabla

my script:

requests.post(url, files={'file[]': open('qwe.txt','rb'), 'file[]':''})

=> got only this (log from Fiddler). One file[] disappears.

--a7fbfa6d52fc4ddd8b82ec8f7055c88b
Content-Disposition: form-data; name="file[]"; filename="qwe.txt"

example content of file qwe.txt blablabla

i tried:

requests.post(url, data={"file[]":""},files={'file[]': open('qwe.txt','rb')})

but its without: filename="" and content-type

--a7fbfa6d52fc4ddd8b82ec8f7055c88b
Content-Disposition: form-data; name="file[]"

--a7fbfa6d52fc4ddd8b82ec8f7055c88b
Content-Disposition: form-data; name="file[]"; filename="qwe.txt"
Content-Type: text/plain

example content of file qwe.txt blablabla

There is any way to add this manually in python-requests?

Upvotes: 0

Views: 607

Answers (1)

mata
mata

Reputation: 69012

Starting with requests 1.1.0 you can use a list of tuples instead of a dict to pass as the files parameter. The first element in each tuple is the name of the multipart form filed, which can be followed either by the content, or by another tuple containing filename, content and (optionally) content type, so in your case:

files = [('file[]', ("", "", "application/octet-stream")),
         ('file[]', ('qwe.txt', open('qwe.txt','rb'), 'text/plain'))]
requests.post(url, files=files)

should produce the result you described.

Upvotes: 1

Related Questions