HTTP PUT request in Python using JSON data

HTTP PUT request in Python using JSON data

Your data is already a JSON-formatted string. You can pass it directly to requests.put instead of converting it with json.dumps again.

Change:

response = requests.put(url, data=json.dumps(data), headers=headers)

to:

response = requests.put(url, data=data, headers=headers)

Alternatively, your data can store a data structure instead, so that json.dumps can convert it to JSON.

Change:

data = [{$key: 8},{$key: 7}]

to:

data = [{$key: 8},{$key: 7}]

HTTP methods in the requests library have a json parameter that will perform json.dumps() for you and set the Content-Type header to application/json:

data = [{$key: 8},{$key: 7}]
response = requests.put(url, json=data)

HTTP PUT request in Python using JSON data

Leave a Reply

Your email address will not be published. Required fields are marked *