The Atlassian Community Forums are currently in read-only mode. We will be relaunching on a new platform on September 22 (read more here). We apologize for the extended downtime. For concerns or questions, please email communitymanagers@atlassian.com. See you on the other side, on the new Atlassian Community Forums! :)

×

Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

how to execute a URL in url customfield in behavior script and show response in another customfield?

rakesh rocckz
August 19, 2019

I have a URL custom field and a text custom field. if someone pastes a URL in the URL custom field it should execute that URL and show the JSON response of that URL in another custom field.is it possible through Behavior scripts? please help me out how to execute a GET request behind the scenes and shows the response in another custom field??

1 comment

Comments for this post are closed

Community moderators have prevented the ability to post new comments.

PD Sheehan
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 Champions.
August 20, 2019

If you add a server-side script to your URL field, you can execute the get and store the result in another form field like this:

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

def url = getFieldById(getFieldChanged()).value
def targetField = getFieldByName("Url fetch result field name")
if(url){
def responseText
def httpBuilder = new HTTPBuilder(url)
httpBuilder.request(Method.GET, ContentType.JSON) {
response.success = {resp, json->
targetField.setFormValue(json.toPrettyString())
}
response.failure = {resp, data ->
targetField.setFormValue("Unable to fetch data from $url. The status was $resp.statusLine. The HTTP GET returned the following text: ${data.toString()}")
}
}
}

Note that this will execute after you focus changes from the URL field.

Also, it will fail if the URL is bad or doesn't return a valid json (this method automatically parses the json into an object). So you may want to wrap the httpBuilder request into a try/catch block.

TAGS
AUG Leaders

Atlassian Community Events