I am trying to repeat the following curl request:
curl -i -k -H "Content-Type: multipart/form-data" \
-F "method=uploadphoto" \
-F "version=1.2.3" \
-F "format=json" \
-F "image[0]=@/Users/user/Downloads/file.jpeg" \
https://example.com/api
with poster usage:
from poster.encode import multipart_encode, MultipartParam
file_content = open('/Users/user/Downloads/file.jpeg', 'rb')
url = 'https://example.com/api'
headers = {
'Content-Type': 'multipart/form-data'
}
parms = {
'method': 'uploadphoto',
'version': '1.2.3',
'format': 'json'
}
mp_parms = []
for name, value in parms.items():
mp_parms.append(MultipartParam(name, value, filetype='text/plain'))
file_photo = MultipartParam('image', file_content, 'file.jpeg', 'application/image')
mp_parms.append(file_photo)
parameters, headers = multipart_encode(mp_parms)
response = urlfetch.Fetch(url, payload=''.join(parameters), headers=headers, method=urlfetch.POST, deadline=60)
But looks like image field is passed as single field, when array should be passed. Usage of image[0] as name doesn't help.
How can I fix it?
I switched the example to use requests because I don't have the Google App SDK installed but the below code works.
One important change was changing
imagetoimage[]to make so the remote end would recognize multiple inputs with the same name. Not sure if the same would apply with urlfetch but I had to add an extra\r\nafter the raw post body.For illustration and testing, I had it upload a second image.
First, the code:
The
test.phpscript it posts to contains<?php var_dump($_POST, $_FILES, $_SERVER);and shows output like:It produced the following POST as grabbed in Wireshark:
I hope that helps. I'm not sure if the API will care, but you might want to base64 encode the image data and set the Content-Encoding to
base64to keep raw binary out of the post.If you're still stuck after integrating some of the changes let me know!