使用 Python 对 REST API 的 PUT 请求

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33127636/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 12:48:59  来源:igfitidea点击:

PUT Request to REST API using Python

pythonrestput

提问by Tim

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

出于某种原因,我的 put 请求不起作用,并且出现语法错误。我是 Python 新手,但我的 GET 和 POST 请求可以正常工作。有没有人看到这个请求和任何建议有什么问题?我正在尝试将描述更改为“已更改的描述”

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'

采纳答案by Muhammad Tahir

Syntax error in because of =sign in your headersdictionary:

由于=在您的headers字典中签名导致语法错误:

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=datais changed with 'data':data. Colon and Single Quotes.

data=data'data':data。冒号和单引号。

And are you sure you will be sending data in your headers? Or you should replace your payloadwith datain your putrequest?

您确定要在标头中发送数据吗?或者你应该更换你payloaddata你的put要求吗?

Edit:

编辑:

As you have edited the question and now you are sending data as PUT request's body requests.put(data=data)so there is no need of it in headers. Just change your headers to:

由于您已经编辑了问题,现在您将数据作为 PUT 请求的正文发送requests.put(data=data),因此标题中不需要它。只需将您的标题更改为:

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

But as you have set your Content-Typeheader to application/jsonso I think in your PUT request you should do

但是由于您已将Content-Type标头设置为application/json所以我认为在您的 PUT 请求中您应该这样做

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

that is send your data as json.

那就是将您的数据作为 json 发送。

回答by Marcus Müller

The problem is that you try to assign datato the dataelement in your dictionary:

问题是您尝试分配datadata字典中的元素:

headers = { ..., data:data }

That can't work because you can't use a dictionary as a key in a dictionary (technically, because it's not hashable).

这是行不通的,因为您不能将字典用作字典中的键(从技术上讲,因为它不可散列)。

You probably wanted to do

你可能想做

headers = { ..., "data":data }