简体   繁体   中英

Creating a JSON post request with python

I am experimenting with the Rapaport Technet API, and want to hit an endpoint which expects the following JSON:

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

Code:

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

I get this response:

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

Any idea what the problem could be?

According to this example from Rapaport Technet API docs, that whole JSON is sent to be as data in the POST request. So simply do the same as given here in Requests docs.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM