python - Using curl to POST obtains a different response than when using requests to POST -


i new python programming , have reached impasse can't seem solve. trying use python requests module to post request service in ibm bluemix. works fine when use curl make request when try requests module error message. since using same data file, confused why getting different results. can enlighten me?

here code access keys , url modified think relevant part of response: here curl:

curl -i -v -h "content-type: application/json" -x post \   "https://ibm.someservice.ibmcloud.com:9999/action/modelname?accesskey=xxxxyyyyyzzzzz+++++etc"  \   --data-binary @sample-data.json 

here request code:

import requests import json  def post2ndrequest():     url = 'https://ibm.someservice.ibmcloud.com:9999/action/modelname?accesskey=xxxxyyyyyzzzzz+++++etc'     files = {'file': open('sample-data.json', 'rb')}     headers = {'content-type':'application/json'}     r = requests.post(url, headers=headers, files=files)     print(r.text)     return r.text  temp = 'pmresponsex.txt' f = open (temp,'wb') f.write(post2ndrequest())   f.close() 

and here chunk of returned response:

<body><h1>http status 500 - org.codehaus.jackson.jsonparseexception: unexpected character ('-' (code 45)) in numeric value: expected digit (0-9) follow minus sign, valid numeric value</h1><hr size="1" noshade="noshade"><p><b>type</b> exception report</p><p><b>message</b> <u>org.codehaus.jackson.jsonparseexception: unexpected character ('-' (code 45)) in numeric value: expected digit (0-9) follow minus sign, valid numeric value</u></p><b>description</b> <u>the server encountered internal error prevented fulfilling request.</u></p> 

it seems sort of parsing error in server yet curl works fine server side seems ok,... have used request module before not using https nor attaching access key url. issue? appreciated!

you using --data-binary option curl, according man page:

this  posts data specified no pro‐ cessing whatsoever. 

whereas in call requests.post using files parameter, according documentation:

:param files: (optional) dictionary of ``'name': file-like-objects``  (or ``{'name': ('filename', fileobj)}``) multipart encoding upload. 

it seems want use data parameter:

:param data: (optional) dictionary, bytes, or file-like object send in body of :class:`request`. 

your current code sending multipart mime archive server expecting different.


Comments

Popular posts from this blog

javascript - oscilloscope of speaker input stops rendering after a few seconds -

javascript - gulp-nodemon - nodejs restart after file change - Error: listen EADDRINUSE events.js:85 -

Fatal Python error: Py_Initialize: unable to load the file system codec. ImportError: No module named 'encodings' -