2

I am trying to upload a file using python requests module and i am not sure whether we can use both data and files in the post call.

fileobj= open(filename,'rb')
upload_data = {
    'data':payload,
    'file':fileobj
}

resp = s.post(upload_url,data=upload_data,headers=upload_headers)

and this is not working. So can anyone help me with this ?

1 Answer 1

1

I think you should be using the data and files keyword parameters in the post request to send the data and file respectively.

with open(filename,'rb') as fileobj:
    files = {'file': fileobj}
    resp = s.post(upload_url,data=payload,files=files,headers=upload_headers)

I've also use a context manager just because it closes the file for me and takes care of exceptions that happen either during file opening or during something that happens with the requests post.

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

3 Comments

Do you know what a context manager is? Take a look at PEP 343 which introduced the with statement
I tried it but it did not work for me. can any one else help me with this ?
@pvg: this is also a part of my problem.

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.