Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 502
Joshua Simon Bloom Avatar asked Mar 07 '26 14:03

Joshua Simon Bloom


1 Answers

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

like image 167
Joshua Simon Bloom Avatar answered Mar 10 '26 03:03

Joshua Simon Bloom



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!