3

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 ????
Cherif Gholla
  • 43
  • 1
  • 1
  • 4
  • Possible duplicate of [How to send a “multipart/form-data” with requests in python?](http://stackoverflow.com/questions/12385179/how-to-send-a-multipart-form-data-with-requests-in-python). – alexpeits Mar 27 '17 at 10:01

1 Answers1

8
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.

skarfa
  • 197
  • 3
  • 13
Attila Kis
  • 521
  • 2
  • 13