JIRA API returns 405 error

George Irwin January 5, 2013

I'm trying to use the REST API to create a new issue in our JIRA system. I've been following the documentation, but the return I get is always "405: Method not allowed". I'm assuming this is a POST/GET issue resulting from being redirected (I've read that POST requests are usually converted to GET requests when they're redirected by the receiving server). I'm working in Django (Python) so am using urllib2 rather than cURL. Here's my code (`theIssue` variable is JSON, omitted for brevity):

myUrl = 'http://MYSUBDOMAIN.atlassian.net/rest/api/latest/issue'
            myRequest = urllib2.Request(myUrl, theIssue)
            myAuthString = base64.standard_b64encode('%s:%s' % ('MYUSER', 'MYPASSWORD'))
            myRequest.add_header("Authorization", "Basic %s" % myAuthString)
            myRequest.add_header("Content-Type", "application/json")
            try:
                theResponse = urllib2.urlopen(myRequest)
                theReturn = simplejson.load(theResponse)

I've played with adding/removing the trailing slash at the end of the URL, but with or without the trailing slash, I always get the 405 error. I'd be really grateful if anyone can point me in the right direction!

EDIT:

As per Renjith's suggestion, I now have:

myRequest = urllib2.Request(myUrl)
            myAuthString = base64.standard_b64encode('%s:%s' % ('MYUSER', 'MYPASS'))
            myRequest.add_header("Authorization", "Basic %s" % myAuthString)
            myRequest.add_header("Content-Type", "application/json")
            try:
                theResponse = urllib2.urlopen(myRequest, theIssue)

But I am still getting 405: Method Not Allowed.

EDIT 2:

Again, thanks to Renjith, I've added https:// to the URL, and now I get 400: Bad Request.....?

EDIT 3 (and SOLUTION):

Renjith saved my bacon here. The first problem was the lack of 's' (as in https://) in my URL (the OnDemand docs are also missing the 's'). The second part of the problem was the formatting of my JSON in Django. I was using variables which I had forgotten to str(). Also, note that I've successfully used json.dumps in place of urllib.urlencode. Another point: my URL does now have a trailing slash, as per the docs.

So with those two solutions, everything is now working!

3 answers

1 accepted

5 votes
Answer accepted
Renjith Pillai
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
January 5, 2013
URL had to be changed to https and also the Json content required modifications.
0 votes
Renjith Pillai
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
January 5, 2013

Hey wait. this appears to be doing a GET. But I am not sure about urllib2.

The code here says, if a second argument is passed to urlopen, a POST is done - http://rest.elkstein.org/2008/02/using-rest-in-python.html

If you are okay with with passing the os_username and os_password parameters to the URL, you can avoid using the request object and just use the url and the json content.

Renjith Pillai
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
January 5, 2013

Oh sorry, I was working on some other REST interface which has a login api and got confused. I have edited the previous comment.

George Irwin January 5, 2013

Let me test the second argument issue first: I had assumed (backed up by other examples) that I could pass the two variables (to force a POST) into the myRequest variable, but you may have picked up the problem.

On the second point: my logging in, do you mean logging into JIRA first? If so, do you mean using urllib2 to log in before sending the content?

George Irwin January 5, 2013

NP Renjith. I've updated the question with the adjusted code, but I'm still getting the same error. Honestly, I don't think it'd be a good idea to pass the username/password to the URL. Unless both ends being SSL make it 100% secure?

Renjith Pillai
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
January 5, 2013

Not really, is what I think. As long as it is not SSL, nothing is secure, even your normal JIRA login. Anyone can decode the header part in the POST requests.

Btw, did you get it working?

George Irwin January 5, 2013

Fair point. I haven't got it working, no. Still getting 405.

Renjith Pillai
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
January 5, 2013

Hey, isn't the URL supposed to have https, OnDemand is on HTTPS, the sample code shows http.

George Irwin January 5, 2013

A very good point - I've added https://, but still no luck.... Still getting 405. I've tried with and without the trailing slash.....

Renjith Pillai
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
January 5, 2013
George Irwin January 5, 2013

Hold on.... Sorry Renjith, it seems I'm now getting 400: Bad Request instead of 405... Misread it, sorry. Will add an edit to the question. Not sure if this gets us any closer?!

Renjith Pillai
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
January 5, 2013

Ah, then may be you should look at the data you are posting (theIssue contents). I am guessing you are adding an issue.

George Irwin January 5, 2013

OK, thanks Renjith - curl is trickier as I'd need to install pycurl. I think the problem is with the json encoding and I think I've solved it by reverting to json.dumps in Django. However, I'm now running into an unrelated issue which is preventing testing; I'll do so and come back tomorrow! Thanks for you help!

George Irwin January 5, 2013

Renjith - got it working! Brilliant. Thank you. I've added the solution to my question as Edit 3, but perhaps you could add another answer saying the problem was the lack of https:// and the formatting of the JSON in Django? I'll then accept that as the answer.

0 votes
Renjith Pillai
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
January 5, 2013

Can you check whether Remote APIs are switched on for you instance in the adminitration?

George Irwin January 5, 2013

Thanks Renjith. Yes they are ON. (Under JIRA Configuration > General Configuration > Allow Remots API Calls is ON)

Suggest an answer

Log in or Sign up to answer