Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

Python, using POST to send XML to REST API

Anthony Anthony October 27, 2011

I am trying to add a command on a build, but I'm getting a "urllib2.HTTPError: HTTP Error 415: Unsupported Media Type" error message. From what I've researched, it seems like the headers are not correct. Is there any documentation or examples of posting a comment on a build?

Here is the Python code that I'm using is:

import urllib2
username = 'achenard'
password = '***'
password_manager = urllib2.HTTPPasswordMgrWithDefaultRealm()
password_manager.add_password(None, 'http://oscbamboo01:8085/', username, password)
auth_handler = urllib2.HTTPBasicAuthHandler(password_manager)
opener = urllib2.build_opener(auth_handler)
urllib2.install_opener(opener)
xmlparameters = '<?xml version="1.0" encoding="UTF-8" standalone="yes" ?><comment author="achenard"><content>A build result comment.</content></comment>'
theurl = 'http://oscbamboo01:8085/rest/api/latest/result/TONY-TONY-44/comment'
req = urllib2.Request(theurl, data=xmlparameters)
req.add_header('Content-Type', 'application/xml; charset=utf-8')
req.add_header('Content-Type', 'text/xml; charset=utf-8')
req.add_header('Content-Length', len(xmlparameters))
handle = urllib2.urlopen(req)

And this is the stacktrace:

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Python27\lib\urllib2.py", line 126, in urlopen
return _opener.open(url, data, timeout)
File "C:\Python27\lib\urllib2.py", line 398, in open
response = meth(req, response)
File "C:\Python27\lib\urllib2.py", line 511, in http_response
'http', request, response, code, msg, hdrs)
File "C:\Python27\lib\urllib2.py", line 436, in error
return self._call_chain(*args)
File "C:\Python27\lib\urllib2.py", line 370, in _call_chain
result = func(*args)
File "C:\Python27\lib\urllib2.py", line 519, in http_error_default
raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)
urllib2.HTTPError: HTTP Error 415: Unsupported Media Type

And I can successful do a GET:

>>> handle = urllib2.urlopen(theurl).read()
>>> print handle
<?xml version="1.0" encoding="UTF-8" standalone="yes"?><comments expand="comments"><link rel="self" href="http://oscbamboo01:8085/rest/api/latest/result/TONY-TONY-44/comment"/><comments expand="comment" size="1" max-result="1" start-index="0"><comment author="achenard"/></comments></comments>

1 answer

1 accepted

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

0 votes
Answer accepted
Przemek Bruski
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
October 29, 2011

req.add_header('Content-Type', 'application/xml; charset=utf-8')
req.add_header('Content-Type', 'text/xml; charset=utf-8')

Why are you adding the content-type twice? Does it work if you set only one?

Anthony Anthony October 30, 2011

I was using both only becuase I saw that on a forum post that I was reading to try to get this working. I just attempted it with only one Content-Type, and text/xml gave me the same Error 415, but application/xml gave me urllib2.HTTPError: HTTP Error 401: Unauthorized. I confirmed that my user was able to still do the GET, is it possible that there is a different permission level that needs to be set for me to do a POST?

Anthony Anthony October 30, 2011

I did some digging on the Error 401, and found a forum post to add in the following:

import urllib2
import base64
username = 'achenard'
password = '***'
password_manager = urllib2.HTTPPasswordMgrWithDefaultRealm()
password_manager.add_password(None, 'http://oscbamboo01:8085/', username, password)
auth_handler = urllib2.HTTPBasicAuthHandler(password_manager)
opener = urllib2.build_opener(auth_handler)
urllib2.install_opener(opener)
xmlparameters = '<?xml version="1.0" encoding="UTF-8" standalone="yes" ?><comment author="achenard"><content>A build result comment.</content></comment>'
theurl = 'http://oscbamboo01:8085/rest/api/latest/result/TONY-TONY-44/comment'
encodedstring = base64.encodestring(username+":"+password)[:-1]
auth = "Basic %s" % encodedstring
headers = {"Authorization": auth}
req = urllib2.Request(theurl, xmlparameters, headers)
req.add_header('Content-Type', 'application/xml; charset=utf-8')
req.add_header('Content-Length', len(xmlparameters))
handle = urllib2.urlopen(req)

I tested this, and it works. I'm not sure that this is the cleanest way that I could do this, but at this time, my issue is resolved. Thanks for your help Przemek.

TAGS
AUG Leaders

Atlassian Community Events