python3 urllib,发出没有数据的 put 请求

问题描述 投票:0回答:1

你好,我有一个 C# 服务,它接受一个没有数据的 put 请求,它只是触发一个算法。

我正在使用 urllib 向此服务发出请求,但是收到 411 错误。这是因为我没有提供数据参数。

req = urllib.request.Request(url, headers=headers, method='PUT')

try:
  response = urllib.request.urlopen(req)

HTTP错误:411

如果我尝试这个:

req = urllib.request.Request(url, headers=headers,DATA="" method='PUT')

try:
  response = urllib.request.urlopen(req)

TypeError:POST 数据应该是字节或字节的可迭代对象。它不能是 str 类型。

有人可以帮助我吗,请不要只说“huuu durr use requests”,我不能使用导入。谢谢你

python-3.x http python-requests urllib
1个回答
6
投票

好的,我解决了这个问题:

req = urllib.request.Request(url, headers=headers,DATA=b'', method='PUT')

try:
  response = urllib.request.urlopen(req)

希望这有帮助

© www.soinside.com 2019 - 2024. All rights reserved.