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 ????
1

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.