简体   繁体   中英

Post Bad Request rest api in python

I am attempting to post some data through REST API in Python.

data.json

    {
      "LastModifiedAt": "2020-12-21T20:19:45.335Z",
       ...
       ...
    }

I am using following code to POST the data.

with open('data.json') as fh:
    data = json.load(fh)
headers = {
    'Content-Type': 'application/json',
    'X-API-Key':'ABC=='
}
response = requests.post('https://myurl.net/api/v1/resource/int_key/endpoint', headers=headers,data=data)

I always get following as response status_code = 400

{
    "ModelState": {
        "line": [
            "Unexpected character encountered while parsing value: L. Path '', line 0, position 0."
        ]
    }, 
    "Message": "The request is invalid."
}

How do I debug this? My URL is correct according to API documentation. Why does it return "Bad Request" status code?

I replaced data with json and it worked.

response = requests.post('https://myurl.net/api/v1/resource/int_key/endpoint', headers=headers, json =data)

I used Postman as suggested by AndroidDev to debug this.

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