How to get REST api from Groovy Script?

Kedar Kanel August 10, 2021

I have created custom REST Endpoint using scriptrunner and its working properly. 

 

Now I want to get the json data from the REST API url created by REST Endpoint using Groovy Script. Can anyone help me on how to do it?

This is the format of my API,

{

total: 1,

values: [

{

jiraid: "BOR-102",

row_id: 166,

row_type: "Recurring",

nor_location_code: "LOC-285212",

}

}

I tried to follow 

https://community.atlassian.com/t5/Marketplace-Apps-Integrations/Call-Rest-API-with-parameters/qaq-p/840702 this thread. 

 

import groovyx.net.http.ContentType
import groovyx.net.http.HTTPBuilder

def httpBuilder = new HTTPBuilder(apiUrl)
log.info(httpBuilder.toString())
httpBuilder.get(
path: "/",
requestContentType: ContentType.JSON,
) {
resp, reader ->
doSomething(resp, reader)
}

def doSomething(def resp, def reader) {
log.info("Resp : " + resp)

log.info("reader : " + reader)
// do something with the data

}

 Executing above code gave me

2021-08-10 12:41:35,686 [ajp-nio-8009-exec-2] | groovyx.net.http.HTTPBuilder@3c4270a3
2021-08-10 12:41:35,793 [ajp-nio-8009-exec-2] | Resp : groovyx.net.http.HttpResponseDecorator@2c73ca93
2021-08-10 12:41:35,793 [ajp-nio-8009-exec-2] | reader : var AJS=AJS||{};AJS.debug=true;
(function() {
var contextPath = '';
var eventBuffer = [];

function printDeprecatedMsg() {
if (console && console.warn) {
console.warn('DEPRECATED JS - contextPath global variable has been deprecated since 7.4.0. Use `wrm/context-path` module instead.');
}
}

function sendEvent(analytics, postfix) {
analytics.send({
name: 'js.globals.contextPath.' + postfix
});
}

function sendDeprecatedEvent(postfix) {
try {
var analytics = require('jira/analytics');
if (eventBuffer.length) {
eventBuffer.forEach(function(value) {
sendEvent(analytics, value);
});
eventBuffer = [];
}

if (postfix) {
sendEvent(analytics, postfix);
}
} catch(ex) {
eventBuffer.push(postfix);
setTimeout(sendDeprecatedEvent, 1000);
}
}

Object.defineProperty(window, 'contextPath', {
get: function() {
printDeprecatedMsg();
sendDeprecatedEvent('get');
return contextPath;
},
set: function(value) {
printDeprecatedMsg();
sendDeprecatedEvent('set');
contextPath = value;
}
});
})();

 

How can I get the json data from the API?

1 answer

0 votes
Martin Bayer _MoroSystems_ s_r_o__
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
August 18, 2021

I think the property of resp you need to use is data. Can you try to check it with?

log.info("Resp : " + resp.data)

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
CLOUD
PRODUCT PLAN
FREE
TAGS
AUG Leaders

Atlassian Community Events