0

I am having some problem with a POST request that is driving me crazy. I am trying to upload an image using a POST using python requests by replicating the original request

First of all, this is the original request I can see with Firebug that the request header is:

Accept application/json, text/javascript, */*; q=0.01
Accept-Encoding gzip, deflate
Accept-Language en-US,en;q=0.7,it;q=0.3
Cache-Control no-cache
Connection keep-alive
Content-Length 26051
Content-Type multipart/form-data; boundary=--------------------------- 37693668010849786771875799013
Cookie some cookie
DNT 1
Host my.host.com
Pragma no-cache
Referer https://my.host.com/postad/1dc185ff9e814068be23ed674956a190
User-Agent Mozilla/5.0 (X11; Linux i686; rv:38.0) Gecko/20100101 Firefox/38.0
X-Requested-With XMLHttpRequest

and the Body of the POST request:

-----------------------------5697279162042627623887894974
Content-Disposition: form-data; name="image"; filename="clip.jpg"
Content-Type: image/jpeg

ÿØÿà�JFIF������ÿÛ�C�     .....
-----------------------------5697279162042627623887894974
Content-Disposition: form-data; name="upload"


-----------------------------5697279162042627623887894974--

This is the code I am using to upload the image:

files = {'image': ('clip.jpg', open('clip.jpg', 'rb'))}
post_image_url = "https://my.host.com/postad/563a533d9105448dbaf853f7ca0265fa/images"
r = session.post(post_image_url, files=files)

but I always get a 404 error, supposing that the request is not correct. Think I have to adjust the request to reflect the origin request examined with Firebug. I believe the request its not complete, I probably am missing the last part of the request:

-----------------------------5697279162042627623887894974
Content-Disposition: form-data; name="upload"


-----------------------------5697279162042627623887894974--

which I cannot replicate in any way.

Any idea?

thank

2 Answers 2

1

Adding this reply if anyone needs it for future.

Please find the below code for sending image file in Python requests. Here logo is the keyword for an image file, which one of our servers accepts as an input. Hence 'logo' is given. Also, make sure you remove 'Content-type' from the headers.

response = requests.post(url,data=data,headers=headers, files={'logo':open("your_image_file.jpeg",'rb')})
1
  • Does this mean that @Giorgio's former answer is now incorrect? Not sure why the additional answer has been added
    – LTPCGO
    Sep 18, 2019 at 17:12
0

I have found the solution, it is quite easy:

 data = {'upload': ''}

I needed to pass the upload string in that way and voilà!

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .