2

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.

1 Answer 1

1

You can add additional fields with the data argument:

data = {'upload': ''}

r = session.post(post_image_url, files=files, data=data)

This is just the value of the submit button, an empty string in your case.

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

Comments

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.