5

I'm trying to upload an image using requests on python. This is what I send using browser

POST /upload-photo/{res1}/{res2}/{res3}/ HTTP/1.1
Host: tgt.tgdot.com
Connection: keep-alive
Content-Length: 280487
Authorization: Basic {value}=
Accept: */*
Origin: http://tgt.tgdot.com
X-Requested-With: XMLHttpRequest
User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.134 Safari/537.36
Content-Type: multipart/form-data; boundary=----WebKitFormBoundaryA8sGeB48ZZCvG127
Referer: http://tgt.tgdot.com/{res1}/
Accept-Encoding: gzip, deflate
Accept-Language: en-US,en;q=0.8,es;q=0.6
Cookie: fttoken={cookie_value}

This is my code

with open(os.getcwd()+"/images/thee1.JPG", "rb") as image_file:
    encoded_image = base64.b64encode(image_file.read())
    headers = {"Content-Type":"multipart/form-data", "Authorization":"Basic " + authvalue}
    cookie = {cookiename: token.value}
    r = requests.post(url, headers =headers, cookies = cookie, params=encoded_image)
    print r.request.headers
    print r.status_code
    print r.text

I keep getting 414 Request-URI Too Large

I'm not sure what's missing here. I would really appreciate help

4
  • How big is the file? And does the API/Server have a max upload size? Commented Jul 23, 2015 at 17:58
  • Image used is 544 kb, server max is 10MB Commented Jul 23, 2015 at 18:00
  • @heinst: it is not the request body that is too large. It is the URI (the URL in this case) that is too large. Commented Jul 23, 2015 at 18:09
  • So why are you base64 encoding the image but claiming to upload a multipart/form-data POST body? Your payload doesn't match your content-type header. Commented Jul 23, 2015 at 18:14

1 Answer 1

6

You are encoding the whole image into the request parameters, effectively extending the URL by the length of the image.

If you already encoded the image data, use the data parameter:

r = requests.post(url, headers=headers, cookies=cookie, data=encoded_image)

Note that requests can encode multipart/form-data POST bodies directly, there is no need for you to encode it yourself. Use the files parameter in that case, passing in a dictionary or sequence of tuples. See the POST Multiple Multipart-Encoded Files section of the documentation.

The library can also handle a username and password pair to handle the Authorization header; simply pass in a (username, password) tuple for the auth keyword argument.

Encoding an image to Base64 is not sufficient however. Your content-type header and your POST payload are not matching. You'd instead post the file with a field name:

with open(os.getcwd()+"/images/thee1.JPG", "rb") as image_file:
    files = {'field_name': image_file}
    cookie = {cookiename: token.value}
    r = requests.post(url, cookies = cookie, files=files, auth=(username, password)
Sign up to request clarification or add additional context in comments.

2 Comments

Cool, that kind of work on constructing the request but now I'm getting Server Error (500)
@PyNaobie: and there is nothing I can do to help you with that, not with the amount of context I have here.

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.