Search code examples
python-3.xhttppython-requestsurllib

python3 urllib, make put request with no data


Hello I have a C# service, that takes a put request with no data, it just triggers an algorithm.

I am using urllib to make a request to this service, however I am getting a 411 error. This is because I am not supplying a data parameter.

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

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

HTTPError: 411

If I try this:

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

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

TypeError: POST data should be bytes or an iterable of bytes. It cannot be of type str.

Can some one please help me, and please dont just say "huuu durr use requests" I cannot use imports. Thank you


Solution

  • Ok I solved this by:

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

    Hope this helps