It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

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

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

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

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

 

I would appreciate any help that anyone could provide!

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

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

Suggest an answer

Log in or Sign up to answer
This widget could not be displayed.
This widget could not be displayed.
Community showcase
Published Apr 03, 2019 in Marketplace Apps

How to estimate projects more accurately?

Gaining insight into your team’s effectiveness and choosing the right estimation technique.   Estimating projects is hard. It is because people have the tendency to underestimate the time thin...

272 views 2 8
Read article

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you