16

I am trying to upload a file using requests. I need to upload a PDF file and at the same time send some other data to the form like the author's name.

I tried this:

requests.get(url, files = {"file":open("file.txt"), "author" : "me" })

But it doesn't send data to the form.

1
  • 1
    @BurhanKhalid please look at the question again Commented Oct 22, 2012 at 16:11

1 Answer 1

34

So I understand that you want to upload to a URL, a pdf file along with some extra parameters.

First error that you have is you are using .get() and not .post().

I am using samples from the documentation, which you should go through. This should get you started:

>>> url = 'http://httpbin.org/post'
>>> files = {'file': open('somefile.pdf', 'rb')}
>>> values = {'author': 'John Smith'}
>>> r = requests.post(url, files=files, data=values)
Sign up to request clarification or add additional context in comments.

5 Comments

I am asked to do this Make a multipart post request to http://server-url/oauth/. with the following form fields. AUTHOR, PDF FILE
should I add the file in data parameter dict.?/?
No, add it to the files parameter, the data parameter is for form data.
@BurhanKhalid - can you also advise how to retrieve this data in an endpoint. I am using flask and get the file contents using request.data. How can I get the additional data (values)?
use request.form in flask endpoint to retrieve the data.

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.