Python requests, unable to upload image to multipart/form-data -
i using python requests upload jpeg image file multipart form.
this see if inspect request made uploading image using 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 python code using upload first image in form (it possible upload 8 images), first should able upload @ least 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 not able upload image.
edit: not same question. question related single specific case: correct request in specific situation? have send multipart request fields inside (as publish images)?
edit2: have found 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 working solution!
Comments
Post a Comment