Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Uploading files using requests and send extra data

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.

like image 938
Dennis Ritchie Avatar asked Oct 22 '12 15:10

Dennis Ritchie


People also ask

How do you upload a file in Python?

Method 1: Using the Python's os Module: Also, the enctype attribute with "multi-part/form-data" value will help the HTML form to upload a file. Lastly, we need the input tag with the filename attribute to upload the file we want. Lastly, we need the input tag with the filename attribute to upload the file we want.


1 Answers

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)
like image 141
Burhan Khalid Avatar answered Sep 19 '22 21:09

Burhan Khalid