4

I'm able to post file using curl

curl -X POST -i -F name='barca' -F country='spain' -F 
file=@/home/messi/Desktop/barca.png 'http://localhost:8080/new_org/hel/concerts'

Which I can get (file) as

 curl -X GET -H 'Accept: image/png' 'http://localhost:8080/new_org/hel/concerts/<id or name of entity>'

But when I tried same thing using requests.post, I got error. Does anybody know why this happen. (Post Error encounter when file pointer is not at last, but when file pointer is at last, I got response 200 but file is not posted)

import requests
url = 'http://localhost:8080/new_org/hel/concerts'
file = dict(file=open('/home/messi/Desktop/barca.png', 'rb'))
data = dict(name='barca', country='spain')
response = requests.post(url, files=file, data=data)

Error: (from usergrid) with response code: 400

{u'duration': 0,
 u'error': u'illegal_argument',
 u'error_description': u'value is null',
 u'exception': u'java.lang.IllegalArgumentException',
 u'timestamp': 1448330119021}

https://github.com/apache/usergrid

Lionel
  • 604
  • 9
  • 26
  • What's the error that you got? – Remi Guan Nov 24 '15 at 03:55
  • Well I don't know, if your `curl` command can works fine, then the Python code maybe can also works. – Remi Guan Nov 24 '15 at 04:08
  • Hmm...let me test your code... – Remi Guan Nov 24 '15 at 04:16
  • There's no problem with your code... – Remi Guan Nov 24 '15 at 04:25
  • Is this problem encounter due to different approach of sending file by curl and requests.post. using curl I only provide file path on my machine and using request, I provide open file. I case for testing purpose, url is : https://api.usergrid.com/narayan0/sandbox/concerts – Lionel Nov 24 '15 at 04:29
  • The POST requests from `curl` and `requests` are *basically* the same; you can compare the results by replacing your URL with `http://httpbin.org/post` and looking at the JSON response that server produces. The issue then must be with `usergrid`. – Martijn Pieters Nov 26 '15 at 08:06
  • 1
    @MartijnPieters I don't understand why post request from curl works and from requests doesn't. Do you have any idea why this type of issue might arise... – Lionel Nov 27 '15 at 07:08
  • 2
    @Pattinson: a bug in the Java parser. What exactly is hard to say; there are subtle differences in the headers and how the multipart/form-data body is formed exactly (differences all well within the HTTP standard). – Martijn Pieters Nov 27 '15 at 07:44
  • @Pattinson: perhaps ask the usergrid project? – Martijn Pieters Nov 27 '15 at 07:45

2 Answers2

9

I believe the problem is that Python is not sending a content-type field for the image that you are sending. I traced through the Usergrid code using a debugger and saw that curl is sending the the content-type for the image and Python is not.

I was able to get this exact code to work on my local Usergrid:

import requests
url = 'http://10.1.1.161:8080/test-organization/test-app/photos/'
files = { 'file': ('13.jpg', open('/Users/dave/Downloads/13.jpg', 'rb'), 'image/jpeg')}
data = dict(name='barca', country='spain')
response = requests.post(url, files=files, data=data)

It is possible that Waken Meng's answer did not work because of the syntax of the files variable, but I'm no Python expert.

snoopdave
  • 306
  • 1
  • 5
1

I met a problem before when i try to upload image files. Then I read the doc and do this part:

You can set the filename, content_type and headers explicitly:

Here is how I define the file_data:

file_data = [('pic', ('test.png', open('test.png'), 'image/png'))]
r = requests.post(url, files=file_data)

file_data should be a a list: [(param_name, (file_name, file, content_type))]

This works for me, hope can help you.

waken meng
  • 21
  • 3
  • 1
    sorry ... as doc said, we can specify filename, content_type and headers explicitly but I don't need to do it... Do you think I need to specify for any reason? also this don't work... problem encounter as before, file not posted – Lionel Nov 27 '15 at 07:06
  • 1
    Curl doesn't set a Content-Type header for the file field either. – Martijn Pieters Nov 27 '15 at 07:43