Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to send a “multipart/form-data” with requests in python?

How to send a multiparta with requests in python?how to post my form with request i try but the post don't work .

Image 1

Image 2

files={'check_type_diff': (None, '0'),
       'category': (None, '19'),
       'company_ad': (None, '0')}


#login
payload = { 'username':'xxxxx','passwd':'xxxx'}
s = requests.Session()
r = s.post('https://exemple.com/0',data=payload)
#login to my account


post ads
r = s.post('https://exemple.com/0', data=files )
print r.text

the last post don't work ????
like image 425
Cherif Gholla Avatar asked Apr 30 '26 09:04

Cherif Gholla


1 Answers

payload = { 'username':'xxxxx',
            'passwd':'xxxx'}
session = requests.Session()
req = session.post('https://exemple.com/0',data=payload)

payload ={'check_type_diff':'0',
          'category':'19',
          'company_ad':'0'}


req = session.post('https://exemple.com/0', data=payload )
print req.content

Note: You should use post('URL',files=files) if you have file content. The multipart data just works as a normal data, just the formatting and the method is not the same.

Example: If you have a file and some multipart data, your code will be like this:

files = {"file":(filename1,open(location+'/'+filename,"rb"),'application-type')}
payload ={'file-name':'Filename',
          'category':'19'}


req = session.post('https://exemple.com/0', data=payload, file=files)
print req.content

You don't even need to add the line "file" into the payload, the requests will put the request together.

like image 140
Attila Kis Avatar answered May 01 '26 21:05

Attila Kis



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!