我需要将json文件发布到http端点。我在这里找到了类似的问题,但是答案对我不起作用。
json文件中的数据是包含json对象的json数组。如何将包含此数据结构的文件发布到http端点?
[{'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'}]
我看到完成将文件发布到http端点的任务的最受欢迎的答案是:
files = {'upload_file': open('file.txt','rb')}
values = {'DB': 'photcat', 'OUT': 'csv', 'SHORT': 'short'}
r = requests.post(url, files=files, data=values)
例如,当我尝试使用下面示例中的代码发布文件时,该文件将失败。
>>> 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)