使用python创建JSON post请求

时间:2016-04-07 16:27:06

标签: python json python-2.7 http-headers request

我正在尝试使用Rapaport Technet API,并希望点击一个需要以下JSON的端点:

{
    "request": {
        "header": {
            "username": "my_username",
            "password": "my_password"
        },
        "body": {}
    }
}

代码:

url = 'https://technet.rapaport.com:449/HTTP/JSON/Prices/GetPriceChanges.aspx'
headers = {'username': 'my_username', 'password': 'my_password'}
r = requests.post(url, headers)

我收到了这个回复:

{
    "response": {
        "header": {
            "error_code": 1001,
            "error_message": "Invalid format"
        },
        "body": {}
    }
}

知道问题可能是什么?

1 个答案:

答案 0 :(得分:0)

根据Rapaport Technet API文档中的example,整个JSON被发送为POST请求中的数据。因此,只需在请求文档中执行相同的here

json_data = {
    "request": {
        "header": {
            "username": "my_username",
            "password": "my_password"
        },
        "body": {}
    }
}
r = requests.post(url, json=json_data)