Unsupported Media type - when trying to pin using python urllib.request

I am trying to pin all the configurations for a project (then I wil also tag them).

I cannot use anything other then python.

I can pin successfully using this code...

base64auth = base64.b64encode("{0}:{1}".format("username", "password").encode("ascii")).decode("ascii").replace("\n","")
url = "http://ipaddress/httpAuth/app/rest/projects/project415/buildTypes"
request = urllib.request.Request(url)
request.add_header("Authorization", "Basic {0}".format(base64auth))
response = urllib.request.urlopen(request)
Configurations = ET.fromstring(response.read().decode('utf-8'))
for config in Configurations.findall('buildType'):
  btCntr +=1
  print ('{0},{1}'.format(config.attrib['name'],"  " + config.attrib['id']))
  request = urllib.request.Request("http://ipaddress/httpAuth/app/rest/builds/?locator=buildType:" + config.attrib['id'] + "&count=10")
  request.add_header("Authorization", "Basic {0}".format(base64auth))
  response = urllib.request.urlopen(request)
  build_info = ET.fromstring(response.read().decode('utf-8'))
  for builds in build_info.findall('build'):
    if builds.attrib['status'] == 'SUCCESS':
      sucCntr +=1
      print ('{0},{1}'.format(builds.attrib['id'],"  " + builds.attrib['status']))
      
      pinurl = "http://ipaddress/httpAuth/app/rest/builds/" + builds.attrib['id'] + "/pin/"
      print ("pinURL: " + pinurl)
      req = urllib.request.Request(pinurl)
      req.get_method = lambda: 'PUT'
      req.add_header("Authorization", "Basic {0}".format(base64auth))
      try: response = urllib.request.urlopen(req)
      except urllib.error.URLError as e:
       print (e.reason)

It is when I change the last group of code above, and I try to add a pin comment that I get an error (415, unsupported media type).

      pindata = "test 1"
      binary_data = pindata.encode('utf-8')  # else get error 'post data should be bytes or an iterable of bytes'
      pinurl = "http://ipaddress/httpAuth/app/rest/builds/" + builds.attrib['id'] + "/pin/"
      print ("pinURL: " + pinurl)
      req = urllib.request.Request(pinurl, binary_data)
      req.get_method = lambda: 'PUT'
      req.add_header("Authorization", "Basic {0}".format(base64auth))
      try: response = urllib.request.urlopen(req)
      except urllib.error.URLError as e:
       print (e.reason)

I think that it has something to do with content type, but I am not sure.  Can someone give me an example.  Thanks

Please sign in to leave a comment.