由于某种原因,我的put请求无法正常工作,并且出现语法错误。我是Python的新手,但我的GET和POST请求正在运行。有人认为这个要求和建议有什么问题吗?我正在尝试将描述更改为“更改的描述”
放
#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)
这是我遇到的错误。
Traceback (most recent call last):
line 17, in <module>
headers = {'Authorization': 'Bearer ' + token, "Content-Type": "application/json", data:data}
TypeError: unhashable type: 'dict'
由于=
登录headers
字典而导致语法错误:
headers = {'Authorization': 'Bearer ' + token, "Content-Type": "application/json", data=data}
它应该是:
headers = {'Authorization': 'Bearer ' + token, "Content-Type": "application/json", 'data':data}
看到data=data
被更改'data':data
。冒号和单引号。
并且确定要在标头中发送数据吗?还是应该在请求中替换payload
为?data
put
编辑:
由于您已经编辑了问题,现在您将数据作为PUT请求的主体发送requests.put(data=data)
,因此在标头中不再需要它。只需将标题更改为:
headers = {'Authorization': 'Bearer ' + token, "Content-Type": "application/json"}
但是,因为您已将Content-Type
标头设置为,application/json
所以我认为在您的PUT请求中您应该
response = requests.put(url, data=json.dumps(data), headers=headers)
将您的数据作为json发送。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句