Reputation: 1405
Posting with postman:
Request Payload:
------WebKitFormBoundaryiPTu5XrX314NHpan
Content-Disposition: form-data; name="photo"; filename="example1.jpg"
Content-Type: image/jpeg
------WebKitFormBoundaryiPTu5XrX314NHpan--
In headers:
Content-Type:multipart/form-data; boundary=----WebKitFormBoundaryiPTu5XrX314NHpan
Need to send images from python app, but getting 400 error everytime.
Tries:
req = urllib2.Request(photoURL, None, {'User-agent' : 'Mozilla/5.0 (Windows; U; Windows NT 5.1; de; rv:1.9.1.5) Gecko/20091102 Firefox/3.5.5'})
imgFile = cStringIO.StringIO(urllib2.urlopen(req).read())
datagen, headers = multipart_encode({"photo": Image.open(imgFile)})
request = urllib2.Request(postPhotoAPI, datagen, headers)
request.add_header("Authorization", authKey)
urllib2.urlopen(request).read()
Result: 400
Same in requests.post and some other post functions (also for base64).
Upvotes: 0
Views: 1282
Reputation: 91
I always use python requests library when i want to make http calls with python.
You need to do something like this:
files = {'photo': ('image.png', open('image.png', 'rb'), 'image/png')}
r = requests.post(photoURL, header={'User-agent' : 'Mozilla/5.0 (Windows; U; Windows NT 5.1; de; rv:1.9.1.5) Gecko/20091102 Firefox/3.5.5', 'Authorization': authKey} files=files)
print r.text
You can read more here.
Upvotes: 2