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

Using JIRA REST api from groovy

Wim Deblauwe
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.
June 20, 2011

Hi,

I am testing the JIRA REST api by writing a small program in Groovy. I am using Groovy 1.8 with HTTPBuilder 0.5.1.

I have this code so far:

import groovyx.net.http.HTTPBuilder
import net.sf.json.JSONArray
 
class Test
{
 
def static JIRA_REST_URL = "http://testserver:8888/jira/rest"
def static JIRA_API_URL = JIRA_REST_URL + "/api/2.0.alpha1/"
 
public static void main(String[] args)
{
def jira = new HTTPBuilder(JIRA_API_URL);
jira.auth.basic "myusername", "mypassword"
 
def serverInfo = jira.get(path: 'serverInfo')
println "Using JIRA version " + serverInfo.version
 
def JSONArray projects = jira.get(path: 'project');
println projects
 
def int numberOfProjects = projects.size()
println "Got $numberOfProjects projects in JIRA";
 
projects.each { println it.dump() }
}
}
Which gives this result:
Using JIRA version 4.3.4
[]
Got 0 projects in JIRA
If I use my browser I do get a list of my JIRA projects. What am I doing wrong?
regards,
Wim

1 answer

1 accepted

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

2 votes
Answer accepted
JamieA
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.
June 21, 2011

Hi Wim,

The source formatter in this thing is pretty dire eh.

I believe the issue is that httpbuilder will only send the basic auth header when challenged, to stop credentials being spewed all over the place. However jira never sends a challenge.

There may be more elegant ways to do this, but, the following works for me. Replace jira.auth.basic(...) with:

jira.client.addRequestInterceptor(new HttpRequestInterceptor() {
void process(HttpRequest httpRequest, HttpContext httpContext) {
httpRequest.addHeader('Authorization', 'Basic ' + 'admin:admin'.bytes.encodeBase64().toString())
}
})

(where admin:admin is username:password).

Making this change made your test class work for me.

(Ref http://en.wikipedia.org/wiki/Basic_access_authentication)

JamieA
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.
June 21, 2011
As an aside... I highly recommend getting Fiddler, then doing: jira.setProxy('localhost', 8888, 'http'). Then you can analyse all the requests/responses.
Wim Deblauwe
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.
June 21, 2011
Thanks for the perfect answer and the tip!
TAGS
AUG Leaders

Atlassian Community Events