lundi 20 juin 2016

How to HTTP push/post JSON data to a set endpoint using Python

I am currently working on a script in Python which will send data from a medical device to be read at a specified endpoint (an address that will receive and compile the data on its own).

The data is contained in a .json file and is ready to be read by the endpoint. The data has already been made written into the .json file in the script, however I am having trouble being able to send the .json file to the endpoint. I am using Python 2.7 and this is currently the code I am using.

register_openers()
datagen, headers = multipart_encode({"data": open("data.json", "rb")})

request = urllib2.urlopen.request("https://exampleaddress.com", datagen, headers)
print urllib2.urlopen(request).read()

With this code I get this error

Traceback (most recent call last):
 File "script.py", line 243, in <module>
request = urllib2.urlopen("https://exampleaddress.com", datagen, headers)
 File "/usr/lib/python2.7/urllib2.py", line 154, in urlopen
return opener.open(url, data, timeout)
 File "/usr/lib/python2.7/urllib2.py", line 429, in open
req = meth(req)
 File "/usr/local/lib/python2.7/dist-packages/poster-0.8.1-py2.7.egg/poster/streaminghttp.py", line 180, in https_request
 ValueError: No Content-Length specified for iterable body

If this is the wrong way to solve the problem, please let me know. Thank you!

Aucun commentaire:

Enregistrer un commentaire