Forums

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

JSON returned from POST response is not in proper format

Shamanth
Contributor
January 9, 2024

Hello,

I am creating an automation rule on Jira that sends a POST request to an external API and the response is stored in a text field. Now the problem I have is that the JSON format is changed when I try to access the body via {{webhookResponse.body}}.

Also, I see the proper JSON format when I try to validate the rule manually before deploying it. I am aware that the automation formats the response so that the JSON values can be accessible via dot notation. But is there a way to access the raw response?

I also have a scriptrunner plugin which I can utilize.

Payload:

Payload.png

 

webhookResponse.body:

Output.png

I somehow need that data as show in the Payload format since I am using it for processing data for other requirements.

3 answers

1 accepted

0 votes
Answer accepted
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 Leaders.
January 10, 2024

In a scriptrunner script (listener, postfunction or job) you can use groovyx.net.http.HTTPBuilder or groovyx.net.http.RESTClient to make post requests to external systems.  

You will have access to the full response

There are almost no limit to what you can do with that.

Shamanth
Contributor
January 16, 2024

Hey Peter,

Thanks for responding!

I am currently having some issues with the RESTClient on my Scriptrunner. I wrote a simple GET request to google.com and when I execute, the circle keeps spinning and it returns nothing. Am I doing something wrong?

Screenshot 2024-01-16 211855.png

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 Leaders.
January 16, 2024

Here is a better example than google (which isn't an api)

2024-01-16 09_07_33-Script Console.png

I don't know why this would just spin on your end. Does your server have outgoing internet access?

Shamanth
Contributor
January 16, 2024

Hey Peter,

It's still the same issue. Nothing ever loads.

But when I try to reach google.com or any URL from the Jira Automation's SEND web request action, I am able to reach it and retrieve the data. Also, I have disabled the allowlist on my Jira.

Let me know if there is anything else that I can check.

Below are the logs that I found in the diagnostic.log

2024-01-16 11:12:59,321+0100 pool-16-thread-1 INFO [atlassian-diagnostics-data-logger] 1705399979307 ; INFO ; HTTP ; HTTP-3001 ; Slow HTTP request detected ; not-detected ; ; ; {"requestPath":"/rest/cb-automation/latest/project/10600/rule/184/execute","username":"shumesh","elapsedTimeInMillis":63704}
2024-01-16 16:54:22,691+0100 pool-16-thread-1 INFO [atlassian-diagnostics-data-logger] 1705420462678 ; INFO ; HTTP ; HTTP-3001 ; Slow HTTP request detected ; not-detected ; ; ; {"requestPath":"/rest/scriptrunner/latest/user/exec/","username":"shumesh","elapsedTimeInMillis":523921}
2024-01-16 17:10:12,960+0100 pool-16-thread-1 INFO [atlassian-diagnostics-data-logger] 1705421412947 ; INFO ; HTTP ; HTTP-3001 ; Slow HTTP request detected ; not-detected ; ; ; {"requestPath":"/rest/scriptrunner/latest/user/exec/","username":"shumesh","elapsedTimeInMillis":130434}
2024-01-17 06:24:27,894+0100 pool-16-thread-1 INFO [atlassian-diagnostics-data-logger] 1705469067881 ; INFO ; HTTP ; HTTP-3001 ; Slow HTTP request detected ; not-detected ; ; ; {"requestPath":"/rest/scriptrunner/latest/user/exec/","username":"shumesh","elapsedTimeInMillis":129990}
2024-01-17 06:50:01,852+0100 pool-16-thread-1 INFO [atlassian-diagnostics-data-logger] 1705470601822 ; INFO ; HTTP ; HTTP-3001 ; Slow HTTP request detected ; not-detected ; ; ; {"requestPath":"/rest/scriptrunner/latest/user/exec/","username":"shumesh","elapsedTimeInMillis":129799}
2024-01-17 07:16:33,129+0100 pool-16-thread-1 INFO [atlassian-diagnostics-data-logger] 1705472193111 ; INFO ; HTTP ; HTTP-3001 ; Slow HTTP request detected ; not-detected ; ; ; {"requestPath":"/rest/scriptrunner/latest/user/exec/","username":"shumesh","elapsedTimeInMillis":131126}
2024-01-17 07:39:23,254+0100 pool-16-thread-1 INFO [atlassian-diagnostics-data-logger] 1705473563241 ; INFO ; HTTP ; HTTP-3001 ; Slow HTTP request detected ; not-detected ; ; ; {"requestPath":"/rest/scriptrunner/latest/user/exec/","username":"shumesh","elapsedTimeInMillis":131193}
2024-01-17 08:07:28,771+0100 pool-16-thread-1 INFO [atlassian-diagnostics-data-logger] 1705475248755 ; INFO ; HTTP ; HTTP-3001 ; Slow HTTP request detected ; not-detected ; ; ; {"requestPath":"/rest/scriptrunner/latest/user/exec/","username":"shumesh","elapsedTimeInMillis":130251}
2024-01-17 08:32:34,014+0100 pool-16-thread-1 INFO [atlassian-diagnostics-data-logger] 1705476754004 ; INFO ; HTTP ; HTTP-3001 ; Slow HTTP request detected ; not-detected ; ; ; {"requestPath":"/rest/scriptrunner/latest/user/exec/","username":"shumesh","elapsedTimeInMillis":130597}

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 Leaders.
January 17, 2024

Sorry, never encountered this type of issue. This goes beyond my expertise.

Do other scripts (without rest calls) complete without issues in the console. 

Shamanth
Contributor
January 17, 2024

Yep, all the other scripts work without any issue. Anyways, I will try asking help from Adaptavist to see if they know what exactly is happening here..

Thanks for your help Peter! 🙂

Shamanth
Contributor
January 20, 2024

Hi @PD Sheehan ,

I figured out that the HTTP requests from Scriptrunner was not passing through the proxy. So I googled up and looked up for a line that sets the RESTClient to use the proxy.

restclient.setProxy('host_url', port, 'http')
I started getting responses successfully!
Now that I am able to successfully POST to a given API, I want to store the returned JSON response to a variable. Unfortunately, the formatting of the JSON is gone. The curly braces have been removed. How can I store that without formatting? Below is the piece of code..
import groovyx.net.http.RESTClient

def url = 'https://myurl'
def authToken = 'my_token'


def restClient = new RESTClient(url)
restClient.setProxy('security-proxy.emea.svc.net', 3128, 'http')

def headers = [
    'Authorization': "Basic $authToken",
    'Content-Type': 'application/x-www-form-urlencoded'
]

def response = restClient.post(headers: headers)
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 Leaders.
January 22, 2024

The RESTClient automatically converts the JSON string into an actual object.

If you only need to work with the underlying data, it's best to keep it as the object. Then you can easily access various elements.

In my example from above, you could do something like this:

import groovyx.net.http.RESTClient

def url = 'https://restcountries.com'
def restClient = new RESTClient(url)
def response = restClient.get(path:'/v3.1/alpha/us', query:[fields:'name']).data
response.name.common

But if you need to just store the json as an actual string elsewhere, then you can easily convert it back:

import groovyx.net.http.RESTClient
import groovy.json.JsonBuilder

def url = 'https://restcountries.com'
def restClient = new RESTClient(url)
def response = restClient.get(path:'/v3.1/alpha/us', query:[fields:'name']).data

def responseJson = new JsonBuilder(response).toString()
//or alternatively
def responseJsonPretty = new JsonBuilder(response).toPrettyString()

 

Like Shamanth likes this
0 votes
Shamanth
Contributor
January 9, 2024

Hi @PD Sheehan

Do you have any working Scriptrunner code that can send a POST request to an external API and retrieve a JSON? Or do you have any other suggestions for my issue mentioned above?

My POST request should include a bearer token and a payload.

Thanks in advance and a Happy New Year! 

0 votes
Gabriel Rodrigues January 9, 2024

Hi @Shamanth

As far as I'm concerned, it's currently not possible to access the payload as RAW JSON in Jira Automation. There's a related feature request for this at https://jira.atlassian.com/browse/AUTO-1016.

Suggest an answer

Log in or Sign up to answer