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

Scriptrunner pulling back External API

Kevin Dalton April 1, 2021

@grab(group='org.codehaus.groovy.modules.http-builder', module='http-builder', version='0.7' )

import groovyx.net.http.RESTClient
import static groovyx.net.http.ContentType.JSON


try{
def rest = new RESTClient('https://status.cernerops.com')
String token = '<bearer token>'
def response = rest.get(
path: '/api/incidents?q=jira2&unresolved=',
headers: [Authorization: "Bearer ${token}"],
requestContentType: JSON )

response.responseData

} catch(Exception ex) {

log.warn "Error: ${ex.message}"

}

 

Keep getting a 401 like the authorization is not passed.  Works in postman but not from my script.

1 answer

Suggest an answer

Log in or Sign up to answer
0 votes
John Chin
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.
April 7, 2021

Hi @Kevin Dalton ,

Do you see the same 401 response error using our library script? If yes, could you please test with curl command in the JIRA server instance and verify if its still hitting the same problem?

Kevin Dalton April 7, 2021

We were able to figure it out.  Stupid Headers ;)

http.request(GET, JSON) { req ->
uri.path = csdincident+svcSearch
headers.'Authorization' = "Bearer $access_token"
headers.'Content-Type' = 'application/json'
headers.'User-Agent' = 'Mozilla/5.0'

}

Like John Chin likes this
Kevin Dalton April 21, 2021

Short and sweet.  We call the API of our incident management applications.


if (tool.toString().contains('JIRA1')) {
def svcSearch = '?service_id=' + JIRA1 + '&unresolved=true'
def GETsearch = ( http.request(GET, JSON) { req ->
uri.path = csdincident+svcSearch
headers.'Authorization' = "Bearer $access_token"
headers.'Content-Type' = 'application/json'
headers.'User-Agent' = 'Mozilla/5.0'

}).toString()
if (GETsearch.size() > 2){
String[] variables = GETsearch .split(",");
String name = variables[1];
def namefix = name.replaceAll("name:","")
String contract = variables[3];
def contractfix = contract.replaceAll("contracts:/api/incidents/","https://status.cernerops.com/incidents/").replaceAll("/contracts]]","").replaceAll("/contracts]","")
HELP = HELP + '<a href="' + contractfix + '">' + namefix + '</a><br><br>'
}}


Then we do some parsing of the JSON to build a nice big red box informing our users on create that there is an incident.


if (HELP.toString()){
HELP = HELP.substring(0, HELP.length() - 8)
ArtInstField.setHelpText('<p style="border:3px; border-style:solid; border-color:#FF0000; padding: 1em;">There is a known incident/s logged to CSD please refer to the following.<br><br>'+ HELP + '</p>')
}
else {
ArtInstField.clearHelpText()
}

 

TAGS
AUG Leaders

Atlassian Community Events