Are you in the loop? Keep up with the latest by making sure you're subscribed to Community Announcements. Just click Watch and select Articles.

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

Earn badges and make progress

You're on your way to the next level! Join the Kudos program to earn points and save your progress.

Deleted user Avatar
Deleted user

Level 1: Seed

25 / 150 points

Next: Root

Avatar

1 badge earned

Collect

Participate in fun challenges

Challenges come and go, but your rewards stay with you. Do more to earn more!

Challenges
Coins

Gift kudos to your peers

What goes around comes around! Share the love by gifting kudos to your peers.

Recognition
Ribbon

Rise up in the ranks

Keep earning points to reach the top of the leaderboard. It resets every quarter so you always have a chance!

Leaderboard

GET Request Always null in ScriptRunner Console

Hello,

I'm trying to test a REST API call with Groovy in the ScriptRunner console but resp is always null. I can get the same content via PowerShell. Anyone know what I'm doing wrong here? 

 

import groovyx.net.http.HTTPBuilder
import net.sf.json.JSONArray
import org.apache.http.HttpRequest;

import org.apache.http.protocol.HttpContext;
import org.apache.http.HttpRequestInterceptor;

def JIRA_API_URL = "https://mydomain.com"

def jira = new HTTPBuilder(JIRA_API_URL);
jira.client.addRequestInterceptor(new HttpRequestInterceptor() {
void process(HttpRequest httpRequest, HttpContext httpContext) {
httpRequest.addHeader('Authorization', 'Basic ' + 'myuser:mypassword'.bytes.encodeBase64().toString())
}
})

def resp = jira.get(path: '/rest/api/2/issue/WO-440', query : [expand:'renderedFields', fields: '"customfield_11101"'])

 

2 answers

Suggest an answer

Log in or Sign up to answer
0 votes
Nir Haimov
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
Feb 12, 2019

Hi @Sir Charles Amante

Why are you using HTTPBuilder?

Here is a complete code that will work for you, just copy paste:

import com.atlassian.jira.component.ComponentAccessor;
import org.apache.commons.httpclient.Credentials;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.UsernamePasswordCredentials;
import org.apache.commons.httpclient.auth.AuthScope;
import org.apache.commons.httpclient.methods.*;
    
def baseUrl = ComponentAccessor.getApplicationProperties().getString("jira.baseurl");  

//rest api request
HttpClient client = new HttpClient();
GetMethod method = new GetMethod(baseUrl + "/rest/api/latest/issue/WO-440?expand=renderedFields&fields=description");
Credentials credentials = new UsernamePasswordCredentials("jira","jira");
client.getParams().setAuthenticationPreemptive(true);
client.getState().setCredentials(AuthScope.ANY, credentials);
client.executeMethod(method);
def response = method.getResponseBodyAsString()
method.releaseConnection();

return response

Thanks Nir! This returns an error about an empty host or something, but I'm not gonna get basic auth working the way I want anyway when traffic's coming in through a load balancer that's enforcing MFA. 

0 votes
Anton Chemlev - Toolstrek -
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.
Feb 11, 2019

Hi,

Try to found why your request fails. For example:

import groovyx.net.http.HTTPBuilder

def jira = new HTTPBuilder("http://yourjira.com")
jira.auth.basic 'user', 'pass'


jira.handler.failure = { resp ->
    " ${resp.statusLine}"
}

def response = jira.get(path: '/rest/api/2/issue/KEY-123')

This way i get "HTTP/1.1 401".

 

 Your code works for me.

Hector
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.
Feb 12, 2019

Hi,

Your code works fine on my side, but the only thing you are not controlling are the possible exception thrown, so I would suggest defining handlers for HTTP errors as mention by @Anton Chemlev - Toolstrek -

Please refer to https://github.com/jgritman/httpbuilder/wiki/Response-Handlers for further details.

Regards, 

Thank you both! It's our KEMP load balancer causing a 403. Crud. Looking into oauth. 

TAGS
AUG Leaders

Atlassian Community Events