2

I am trying to make a multipart/form-data post request using python "requests" library the request payload is this

------WebKitFormBoundaryFpBudhNO5RIwvCAv
Content-Disposition: form-data; name="token"

nwcnfie_3c59dc048e8850243be8079a5c7
------WebKitFormBoundaryFpBudhNO5RIwvCAv
Content-Disposition: form-data; name="uid"

3f7316b6fee4de110e0ab5db7ccdd9df9373
------WebKitFormBoundaryFpBudhNO5RIwvCAv
Content-Disposition: form-data; name="terms"

1
------WebKitFormBoundaryFpBudhNO5RIwvCAv
Content-Disposition: form-data; name="movie_title637ee6f6c51cef5a4887c67116bd7375"

this one is the most awesome movie
------WebKitFormBoundaryFpBudhNO5RIwvCAv
Content-Disposition: form-data; name="description637ee6f6c51cef5a4887c67116bd7375"


------WebKitFormBoundaryFpBudhNO5RIwvCAv
Content-Disposition: form-data; name="files[]"; filename="Time Value of Money -    YouTube.FLV"
Content-Type: video/x-flv


------WebKitFormBoundaryFpBudhNO5RIwvCAv--

Here is how I am trying to POST this

files = {'files[]': ("filename", open('Time Value of Money -    YouTube.FLV', 'rb'))}
upload_payload = {"token":"nwcnfie_"+getUploadToken(user_page.text),"uid":getUID(user_page.text),"terms":"1","movie_title"+getFileNameHash("Time Value of Money -    YouTube.FLV"):"This is a really awesome movie","description"+getFileNameHash("Time Value of Money -    YouTube.FLV"):"Awesome"}
response = session.post("http://example.com/uploader.php",data=upload_payload,files=files)

Is this the correct way of doing the request especially the file part? I am not getting the result I was expecting. Please help.

meadhikari
  • 971
  • 3
  • 13
  • 27
  • 2
    You are using `login_payload` in your `session.post()`, not `upload_payload` that you built on the line preceding the call. – Martijn Pieters Jan 15 '15 at 15:45
  • 1
    Other than that, yes, you are using the `requests` library correctly to build a POST body from a file and other fields. Without more information of where you are posting to, what they expect and what happens instead we cannot help you. – Martijn Pieters Jan 15 '15 at 15:52
  • @MartijnPieters Hi, thanks for the info, it was just typo here. Is the way I am referring to the file correct? I mean the name and filename field at the last form-data. – meadhikari Jan 15 '15 at 15:57
  • Yes, that's entirely correct. A two-element tuple is interpreted as *filename, fileobject*. You could *prepare* your request first (see http://docs.python-requests.org/en/latest/user/advanced/#prepared-requests), then look at `prepped.body` if you want to see exactly what'll be posted. – Martijn Pieters Jan 15 '15 at 16:00
  • 1
    Another way to debug is to post to `http://httpbin.org/post` and see what JSON information comes back about what you posted. – Martijn Pieters Jan 15 '15 at 16:00
  • Actually my problem was using "filename" instead of the actual filename to upload the file – meadhikari Jan 16 '15 at 10:55
  • Ah, I thought you were using that as an example.. :-P – Martijn Pieters Jan 16 '15 at 10:58

0 Answers0