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!
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Oh sorry, I was working on some other REST interface which has a login api and got confused. I have edited the previous comment.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Fair point. I haven't got it working, no. Still getting 405.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hey, isn't the URL supposed to have https, OnDemand is on HTTPS, the sample code shows http.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
A very good point - I've added https://, but still no luck.... Still getting 405. I've tried with and without the trailing slash.....
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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?!
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Ah, then may be you should look at the data you are posting (theIssue contents). I am guessing you are adding an issue.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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!
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Can you check whether Remote APIs are switched on for you instance in the adminitration?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Thanks Renjith. Yes they are ON. (Under JIRA Configuration > General Configuration > Allow Remots API Calls is ON)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Spend the day sharpening your skills in Atlassian Cloud Organization Admin or Jira Administration, then take the exam onsite. Already ready? Take one - or more - of 12 different certification exams while you’re in Anaheim at Team' 25.
Learn more
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.