I have a need to post a json file to an http endpoint. I have found similar questions here, however the answers are not working for me.
The data in the json file is a json array containing json objects. How can I post a file containing this data structure to an http endpoint?
[{'app': 'AccessoryService',
'name': 'accessoryservice-1-3-pf2a-86c9d6db8d-lgbxf',
'restart': 0,
'routeroffer': 'PF2A',
'state': {'running': {'startedAt': '2019-07-19T14:45:20Z'}},
'version': '1.3.3'},
{'app': 'AktivateUserInterface',
'name': 'aktivateuserinterface-1-3-pf2a-64bc9b5bc-9r5m4',
'restart': 0,
'routeroffer': 'PF2A',
'state': {'running': {'startedAt': '2019-08-26T20:07:43Z'}},
'version': '1.3.128'},
{'app': 'ApprovalServices',
'name': 'approvalservices-1-3-pf2a-5764f4ff44-mmnbx',
'restart': 1,
'routeroffer': 'PF2A',
'state': {'running': {'startedAt': '2019-08-21T19:30:53Z'}},
'version': '1.3.3'}]
The most popular answer I've seen to accomplish the task of posting a file to the http endpoint is this:
files = {'upload_file': open('file.txt','rb')}
values = {'DB': 'photcat', 'OUT': 'csv', 'SHORT': 'short'}
r = requests.post(url, files=files, data=values)
For example when I try to post a file using the code in the example below it fails.
>>> import requests
>>> file = {'upload': open('test.json', 'rb')}
>>> values = {"name": "Dwayne", "age": "34"}
>>> resp = requests.post('http://135.47.242.85:12000/api/files', files=file, data=values)
>>> resp
<Response [400]>
>>> resp.json()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Users\dc867r\AppData\Local\Programs\Python\Python37\lib\site-packages\requests\models.py", line 897, in json
return complexjson.loads(self.text, **kwargs)
File "C:\Users\dc867r\AppData\Local\Programs\Python\Python37\lib\json\__init__.py", line 348, in loads
return _default_decoder.decode(s)
File "C:\Users\dc867r\AppData\Local\Programs\Python\Python37\lib\json\decoder.py", line 337, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "C:\Users\dc867r\AppData\Local\Programs\Python\Python37\lib\json\decoder.py", line 355, in raw_decode
raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)