gpt4 book ai didi

python - 用Python重写Curl请求: Result Invalid Json

转载 作者:行者123 更新时间:2023-12-01 00:46:07 25 4
gpt4 key购买 nike

我有这个可以工作的 curl ,它返回这个

curl -X POST \
https://login.smoobu.com/booking/checkApartmentAvailability \
-H 'Api-Key: xxxxx' \
-H 'cache-control: no-cache' \
-d '{
"arrivalDate" : "2018-04-01",
"departureDate": "2019-12-03",
"apartments": [126936, 127858, 126937],
"customerId": 38484
}'

它返回这个

{
"availableApartments": [
127858
],
"prices": [],
"errorMessages": {
"126936": {
"errorCode": 405,
"message": "The chosen day of departure is not available.",
"departureDays": [
"Sa"
]
},
"126937": {
"errorCode": 405,
"message": "The chosen day of departure is not available.",
"departureDays": [
"Sa"
]
}
}
}

我像这样用Python重写了它

In [1]: import requests

In [2]: headers = {'Api-Key': 'xxxx', 'cache-control': 'no-cache'}

In [8]: payload = {
...: "arrivalDate" : "2018-04-01",
...: "departureDate": "2019-12-03",
...: "apartments": [126936, 127858, 126937],
...: "customerId": 38484
...: }

In [4]: r = requests.post("https://login.smoobu.com/booking/checkApartmentAvailability", data=payload, headers=headers)

In [5]: r
Out[5]: <Response [400]>

In [13]: r.content
Out[13]: b'{"title":"Error occurred","detail":"json is invalid"}'

我收到了无效 json 的响应,但我不确定为什么,因为我知道它的工作方式是它需要一个字典并将其转换为 json 请求。

最佳答案

要发送 JSON,您应该使用 json 参数:

r = requests.post("https://login.smoobu.com/booking/checkApartmentAvailability", json=payload, headers=headers)

docs 中所述:

r = requests.post(url, data=json.dumps(payload))
r = requests.post(url, json=payload)

Instead of encoding the dict yourself, you can also pass it directly using the json parameter (added in version 2.4.2) and it will be encoded automatically

关于python - 用Python重写Curl请求: Result Invalid Json,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56964844/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com