PUT REST API request using Python

For some reason, my placement request is not working and I am getting syntax errors. I am new to Python, but I have my GET and POST requests. Does anyone see something wrong with this request and any recommendations? I am trying to change the description to "Modified Description"

Put

#import requests library for making REST calls import requests import json #specify url url = 'my URL' token = "my token" data = { "agentName": "myAgentName", "agentId": "20", "description": "Changed Description", "platform": "Windows" } headers = {'Authorization': 'Bearer ' + token, "Content-Type": "application/json", data:data} #Call REST API response = requests.put(url, data=data, headers=headers) #Print Response print(response.text) 

Here is the error I am getting.

 Traceback (most recent call last): line 17, in <module> headers = {'Authorization': 'Bearer ' + token, "Content-Type": "application/json", data:data} TypeError: unhashable type: 'dict' 
+5
source share
2 answers

Syntax error due to = in your headers dictionary:

 headers = {'Authorization': 'Bearer ' + token, "Content-Type": "application/json", data=data} 

It should be:

 headers = {'Authorization': 'Bearer ' + token, "Content-Type": "application/json", 'data':data} 

See data=data changed using 'data':data . Colon and Single Quotes.

And are you sure you will send data to your headers? Or should you replace payload with data in the put request?

Edit:

As you edited the question, and now you send the data as the request body PUT requests.put(data=data) , so there is no need for headers. Just change the headers to:

 headers = {'Authorization': 'Bearer ' + token, "Content-Type": "application/json"} 

But since you set the Content-Type header to application/json , so I think in the PUT request you should do

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

which sends your data as json.

+3
source

The problem is that you are trying to assign data to the data element in the dictionary:

 headers = { ..., data:data } 

This may not work because you cannot use the dictionary as a key in the dictionary (technically, because it is not hashed).

Perhaps you wanted to do

 headers = { ..., "data":data } 
+1
source

Source: https://habr.com/ru/post/1233706/


All Articles