0

I am using python requests to upload a jpeg image file to a multipart form.

This is what I see if I inspect the request made by normally uploading the image using the browser:

------WebKitFormBoundaryKGkGxMtaz943aFXQ
Content-Disposition: form-data; name="uploadphotos"

true
------WebKitFormBoundaryKGkGxMtaz943aFXQ
Content-Disposition: form-data; name="deletephoto"

0
------WebKitFormBoundaryKGkGxMtaz943aFXQ
Content-Disposition: form-data; name="upload_input_0"; filename="1 KIN 
GRD.jpg"
Content-Type: image/jpeg


------WebKitFormBoundaryKGkGxMtaz943aFXQ
Content-Disposition: form-data; name="upload_path0"

C:\fakepath\1 KIN GRD.jpg
------WebKitFormBoundaryKGkGxMtaz943aFXQ
Content-Disposition: form-data; name="e$phMain$UploadButton0"

Upload photo
------WebKitFormBoundaryKGkGxMtaz943aFXQ
Content-Disposition: form-data; name="upload_input_1"; filename=""
Content-Type: application/octet-stream


------WebKitFormBoundaryKGkGxMtaz943aFXQ
Content-Disposition: form-data; name="upload_path1"


------WebKitFormBoundaryKGkGxMtaz943aFXQ
Content-Disposition: form-data; name="upload_input_2"; filename=""
Content-Type: application/octet-stream


------WebKitFormBoundaryKGkGxMtaz943aFXQ
Content-Disposition: form-data; name="upload_path2"


------WebKitFormBoundaryKGkGxMtaz943aFXQ
Content-Disposition: form-data; name="upload_input_3"; filename=""
Content-Type: application/octet-stream


------WebKitFormBoundaryKGkGxMtaz943aFXQ
Content-Disposition: form-data; name="upload_path3"


------WebKitFormBoundaryKGkGxMtaz943aFXQ
Content-Disposition: form-data; name="upload_input_4"; filename=""
Content-Type: application/octet-stream


------WebKitFormBoundaryKGkGxMtaz943aFXQ
Content-Disposition: form-data; name="upload_path4"


------WebKitFormBoundaryKGkGxMtaz943aFXQ
Content-Disposition: form-data; name="upload_input_5"; filename=""
Content-Type: application/octet-stream


------WebKitFormBoundaryKGkGxMtaz943aFXQ
Content-Disposition: form-data; name="upload_path5"


------WebKitFormBoundaryKGkGxMtaz943aFXQ
Content-Disposition: form-data; name="upload_input_6"; filename=""
Content-Type: application/octet-stream


------WebKitFormBoundaryKGkGxMtaz943aFXQ
Content-Disposition: form-data; name="upload_path6"


------WebKitFormBoundaryKGkGxMtaz943aFXQ
Content-Disposition: form-data; name="upload_input_7"; filename=""
Content-Type: application/octet-stream


------WebKitFormBoundaryKGkGxMtaz943aFXQ
Content-Disposition: form-data; name="upload_path7"


------WebKitFormBoundaryKGkGxMtaz943aFXQ--

This is the python code I am using to upload the first image in the form (it is possible to upload up to 8 images), but first I should be able to upload at least the first one:

files = {'upload_input_0': ("test_image.jpg", open('test_image.jpg', 'rb'),'image/jpg')}
r = requests.post(upload_image_url, headers = headers, files=files)

but I am not able to upload the image.

EDIT: this is not the same question. My question is related to this single specific case: what is the correct request in this specific situation? do I have to send the multipart request with ALL the fields inside (as I would publish all the images)?

EDIT2: I have found the solution:

multipart_form_data = {
        'uploadphotos': (None, 'true', None),
        'deletephoto': (None, '0', None),
        'upload_input_0': ("test2.jpg", open('test2.jpg', 'rb'),'image/jpeg'),
        'upload_path0': (None, 'C:\fakepath\test2.jpg', None),
        'e$phMain$UploadButton0': (None, 'Upload photo', None),
        'upload_input_1': ('', ''),
        'upload_path1': (None, '', None),
        'upload_input_2': ('', ''),
        'upload_path2': (None, '', None),
        'upload_input_3': ('', ''),
        'upload_path3': (None, '', None),
        'upload_input4': ('', ''),
        'upload_path4': (None, '', None),
        'upload_input_5': ('', ''),
        'upload_path5': (None, '', None),
        'upload_input_6': ('', ''),
        'upload_path6': (None, '', None),
        'upload_input_7': ('', ''),
        'upload_path7': (None, '', None),
    }
    r = s.post(upload_image_url, headers = headers_s, files=multipart_form_data)

Which is a perfectly working solution!

giorgio
  • 21
  • 2
  • 4
  • @stovfl I have done that and it is exactly what I am doing – giorgio Sep 12 '17 at 14:36
  • "but I am not able to upload the image." - Can you provide some more info on the error you're seeing, what exactly isn't working, etc? – Tom Dalton Sep 12 '17 at 14:51
  • @TomDalton no errors whatsoever, I get 200 as response, so everything seems fine but the image is not being uploaded. I don't have access to the server log. – giorgio Sep 12 '17 at 14:58
  • @stovfl in the page you linked before, in the section POST a Multipart-Encoded File, it says we have to use files. Am I missing something? – giorgio Sep 12 '17 at 15:00
  • Possible duplicate of [How to send a "multipart/form-data" with requests in python?](https://stackoverflow.com/questions/12385179/how-to-send-a-multipart-form-data-with-requests-in-python) – stovfl Sep 12 '17 at 16:00

0 Answers0