我在API POST上有问题,你能帮我吗?
import requests
import json
url = 'http://xxx/api/getTotalPrice'
param = dict(itineraryType=1,
departureAirportCode='HAN',
destinationAirportCode='DLI',
departureDate='2020-12-30T14:00',
returnDate='2020-12-30T09:00',
adult=1,
children=1,
infant=1
)
resp = requests.post(url=url, params=param)
u_data = resp.json()
print(u_data)
我想从API POST接收数据。这是主体API,这产生了错误。
这里,这是错误
C:UsersAdminPycharmProjectsuntitledvenvScriptspython.exe C:/Users/Admin/PycharmProjects/untitled/venv/test.py
Traceback (most recent call last):
File "C:UsersAdminPycharmProjectsuntitledvenvtest.py", line 17, in <module>
u_data = resp.json()
File "C:UsersAdminPycharmProjectsuntitledvenvlibsite-packagesrequestsmodels.py", line 900, in json
return complexjson.loads(self.text, **kwargs)
File "C:UsersAdminAppDataLocalProgramsPythonPython39libjson__init__.py", line 346, in loads
return _default_decoder.decode(s)
File "C:UsersAdminAppDataLocalProgramsPythonPython39libjsondecoder.py", line 337, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "C:UsersAdminAppDataLocalProgramsPythonPython39libjsondecoder.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)
Process finished with exit code 1
我可以在没有body的情况下从POST API获取数据,但有了body,我就不能了。你能帮助修复代码吗?
非常感谢!
p/S:这里的值,我想得到
{
"departureFlight": {
"totalPrice": 1896000.0,
"airlineCode": "VN"
},
"returnFlight": {
"totalPrice": 3263800.0,
"airlineCode": "VJ"
}
}
好的,我有答案了。
data = {'itineraryType':1,'departureAirportCode':'HAN','destinationAirportCode':'DLI','departureDate':'2020-12-30T15:00','returnDate':'2020-12-30T09:00','adult':1,'children':1,'infant':1}
headers={'Content-type':'application/json', 'Accept':'application/json'}
resp = requests.post(url=url, json=data, headers=headers)
u_data = resp.json()
print(u_data)
将dict(…(更改为{…}并设置标头。