Reputation: 43
How to send a multiparta with requests in python?how to post my form with request i try but the post don't work .
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 ????
Upvotes: 3
Views: 26227
Reputation: 533
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.
Upvotes: 9