Come for the products,
stay for the community

The Atlassian Community can help you and your team get more value out of Atlassian products and practices.

Atlassian Community about banner
4,368,486
Community Members
 
Community Events
168
Community Groups

Scriptrunner pulling back External API

@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

0 votes

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?

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

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()
}

 

Suggest an answer

Log in or Sign up to answer
TAGS

Atlassian Community Events