带有 URL 路径变量的 Python http 删除请求

Ans*_*rma 0 python path-variables python-requests

我有一个公开为 http://localhost:8080/test/api/v1/qc/{id} 的 Http 端点用于删除,在进行此 API 删除调用时,我必须用正确的 id 替换

我使用 python 的 requests 模块尝试了以下方法

param = {
  "id" : 1
}

requests.delete(url = http://localhost:8080/test/api/v1/qc/{id}, params=param)
Run Code Online (Sandbox Code Playgroud)

此 API 调用因错误而中断

ValueError: No JSON object could be decoded.
Run Code Online (Sandbox Code Playgroud)

我怎样才能做到这一点?

Foo*_*o L 5

您的代码无法按原样运行。您需要引用您的网址字符串:

url = "http://localhost:8080/test/api/v1/qc/{id}"

阅读requests 的文档params只会将字典param作为查询字符串发送,因此它只会附加?id=1到 URL 的末尾。

您想要的是{id}从字典中获取值。您可以通过多种方式查看此答案:How do I format a string using adictionary in python-3.x?

你想要类似的东西

requests.delete(url = "http://localhost:8080/test/api/v1/qc/{id}".format(**param))