在Python中重写Curl请求:结果无效Json

在Python中重写Curl请求:结果无效Json,python,curl,Python,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, 1

我有一个有效的旋度,它返回这个

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"
            ]
        }
    }
}
它返回这个

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"}'
[1]中的
:导入请求
在[2]中:headers={'Api-Key':'xxxx','cache control':'no cache'}
在[8]中:有效载荷={
…:“到达日期”:“2018-04-01”,
…:“出发日期”:“2019-12-03”,
…:“公寓”:[126936127858126937],
…:“customerId”:38484
...: }
在[4]中:r=requests.post(“https://login.smoobu.com/booking/checkApartmentAvailability,数据=有效负载,标题=标题)
[5]:r
出[5]:
在[13]中:r.content
Out[13]:b'{“title”:“出错”,“详细信息”:“json无效”}

我得到了一个无效json的响应,我不知道为什么,因为我知道它的工作方式是它接受一个dict并将其转换为json请求。

对于发送
json
,您应该使用
json
参数:

r=requests.post(“https://login.smoobu.com/booking/checkApartmentAvailability,json=payload,headers=headers)
正如报告中所述:

r=requests.post(url,data=json.dumps(有效负载))
r=requests.post(url,json=payload)
您也可以使用json参数(在版本2.4.2中添加)直接传递dict,而不是自己编码dict,它将自动编码


这是有意义的,所以它以另一种方式作为基本的post字符串请求发送。顺便说一句,它很管用。