Make a GET request to REST API and Handle JSON in behavior plugin serverside script JIRA

Vineet Shah June 29, 2017

I want to be able to write a serverside script in the JIRA behavior plugin inside ScriptRunner:

https://scriptrunner.adaptavist.com/latest/jira/behaviours-overview.html

The script should be able to hit an endpoint which has some json and parse it to set the text of a field in a jira create issue view. I could not figure out a way to make an ajax call either.

import groovy.json.JsonSlurper

def desc = getFieldById("description")
def jsonSlurper = new JsonSlurper()
/* MAKE A GET REQUEST HERE TO API URL AND PARSE THE
RESPONSE DATA TO SET THE TEXT VALUE OF defaultValue.
I AM NOT SURE IF JSONSLURPER IS THE RIGHT WAY TO GO */
def defaultValue = out.value desc.setFormValue(defaultValue)

1 answer

0 votes
Daniel Yelamos [Adaptavist]
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.
July 5, 2017

Hi Vineet!

I recently posted an answer about this that you can find here.

If that question doesn't help you, please do tell and I'll try to provide more guidance. 

May I advice you to search the community before posting new questions? Most of the stuff that people think about has already been answered in some way.

Cheers!

Dyelamos

Vineet Shah July 5, 2017

Hey Dyelamos,

Thank you for the guidance. I posted on here because I am still not sure if I am doing it right.

Here is my following code, the URL is not reachable from outside the network so I put in a temporary URL. I know that I can reach my URL correctly as I am able to populate a dropdown through the ajaxoptions parameter in the convertToSingleSelect method.

import groovyx.net.http.HTTPBuilder

def cusField = getFieldById(getFieldChanged())
def val = cusField.getValue()
def desc = getFieldById("description")

def http = new HTTPBuilder( 'http://example.org/project/' )
 
http.get( path: 'getFieldByService/' + val ) { resp, json ->
    desc.setFormValue(json.items[0].fields[0].name)
}

This is the format of an example json response if val was "Snowball".  The full path of the url would be:

'http://example.org/project/getFieldByService/Snowball'

{"items":[{"fields":[{"name":"Job ID: "},{"name":"Edge Service Name: "}]}]}

I am unable to access this data and am looking for any assistance that you may be able to provide.

 

Thanks,

Vineet

 

Vineet Shah July 10, 2017

I would appreciate any help that anyone could provide!

Daniel Yelamos [Adaptavist]
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.
July 11, 2017

Hi Vineet.

As I can see from the code you provided, you are still not using the right syntax nor code. Did you take a look at the code the user from the question I posted?

He uses this HTTPBuilder module to craft his POST request

import groovyx.net.http.HTTPBuilder
import static groovyx.net.http.ContentType.*
import groovyx.net.http.ContentType
import static groovyx.net.http.Method.*
import groovy.json.JsonSlurper
import net.sf.json.groovy.JsonSlurper

def http = new HTTPBuilder('http://IP/authentication')
http.request(POST) {
    requestContentType = ContentType.JSON
    body =  [username: 'USERNAME', password: 'PASSWORD']

        response.success = { resp, JSON ->

  return JSON
            
    }
    
        response.failure = { resp ->
        return "Request failed with status ${resp.status}"
            
            }
    }

Could you adapt that code to what you already have?

The JSON variable in the response should contain a String with the data from your response.

Cheers.

Dyelamos

Vineela Durbha April 18, 2019

Hi @Daniel Yelamos [Adaptavist] 

 I have tried to use above code and it is throwing multiple errors like

RequestContentType undeclared ..body not declared..

no such method called success ..

 

I am getting all the above errors. Can you please help me in resolving this

Like # people like this

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events