0

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)
Teymour
  • 1,832
  • 1
  • 13
  • 34
dcrearer
  • 1,972
  • 4
  • 24
  • 48
  • Can you print the response? I think it is not a valid JSON format. – alec_djinn Aug 29 '19 at 16:24
  • could you put up what `resp.text` says? – rohit keshav Aug 29 '19 at 16:25
  • The response is above for a simple json object – dcrearer Aug 29 '19 at 16:26
  • it says 'file is required' – dcrearer Aug 29 '19 at 16:26
  • replace with `file = {'file': open('test.json', 'rb')}` and try again, it might be cause its expecting a key called `file` and you are supplying it with `upload` – rohit keshav Aug 29 '19 at 16:30
  • files = { 'json': (None, json.dumps(Values), 'application/json'), 'file': (os.path.basename(file), open(file, 'rb'), 'path') } not 100% try something like this, maybe make a function for send? – Stian Diehard Aug 29 '19 at 17:18
  • awesome, that works... so how would I post a file that contains an array of json objects? Is it necessary to do this for every json object in the array - values = {"name": "Dwayne", "age": "34"} – dcrearer Aug 29 '19 at 17:28
  • tired fingers pressed enter by mistake, I found this hope it helps : [link](https://stackoverflow.com/questions/35939761/how-to-send-json-as-part-of-multipart-post-request#comment59538358_35939761) _italic_ – Stian Diehard Aug 29 '19 at 17:30
  • It is possible, I dont know how forehand but if you re-edit the post with current code I am sure more people will be able to help. I am trying to think but the first idea I come up with is simply looping over a regular list (not a tuple) with filenames instead of values and put everything in a function. Sorry I could not be of more help. – Stian Diehard Aug 29 '19 at 18:31

0 Answers0