Forums

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

POST request using groovy in scriptrunner outputs to null

John Michael Sunga
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
February 6, 2024

I’m trying to get jwt token in helix/bmc remedy using script console but it always outputs to null without logs. I’ve hidden the confidential informations

IMG_1227.jpeg

 

1 answer

0 votes
Ram Kumar Aravindakshan _Adaptavist_
Community Champion
February 7, 2024

Hi @John Michael Sunga

Could you please share your code in a text format, so I can review it and provide some feedback?

Also, if you are trying to connect to an external REST Service, I suggest using this Adaptavist Library example.

I am looking forward to your feedback.

Thank you and Kind regards,
Ram

 

John Michael Sunga
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
February 7, 2024

import groovyx.net.http.HTTPBuilder

import groovyx.net.http.Method

import groovyx.net.http.ContentType

 

def http = new HTTPBuilder("https://www-mbl-test:11443")

       def data = http.request(Method.POST,ContentType.URLENC){ req ->

       uri.path = "/api/jwt/login"

       body = [username:"USERNAME", password:"PASSWORD"]

       response.success = { resp, jsonData ->

             log.error("success"+resp.statusLine);

             assert resp.status == 200

             return jsonData 

            }

       response.failure = {resp ->

         log.error("failure")

         return null

            }

       }

Ram Kumar Aravindakshan _Adaptavist_
Community Champion
February 9, 2024

Hi @John Michael Sunga

For your requirement, you should instead try using a GET request instead of a POST request.

Below is a sample code for your reference:-

import groovy.json.JsonOutput
import groovyx.net.http.ContentType
import groovyx.net.http.HttpResponseDecorator
import groovyx.net.http.RESTClient

final externalUrl = "https://www-mbl-test:11443"

def getResponse = get(externalUrl, '/api/jwt/login', '')

if (getResponse) {
def responseGetMap = getResponse as Map
responseGetMap
}

def get(def hostUrl, def endpoint, def query) {
if (hostUrl.toString().contains('&') || hostUrl.toString().contains('?')) {
log.error 'The parameters of the endpoint must be included in the query variable'
return
}
def username = '<YOUR_USERNAME>'
def password = '<YOUR_PASSWORD>'

def client = new RESTClient(hostUrl)
def headers = ['Authorization': "Basic ${"${username}:${password}".bytes.encodeBase64()}", 'Accept': 'application/json'] as Map
client.setHeaders(headers)

client.handler.success = { HttpResponseDecorator response, json ->
log.warn "======>>>> ${response.statusLine}"
log.warn "======>>>> ${json}"
json
}
client.handler.failure = { HttpResponseDecorator response ->
log.error response.entity.content.text
[:]
}
client.get(path: endpoint, queryString: query, contentType: ContentType.JSON )
}

Please note that the sample code above is not 100% exact to your environment. Hence you will need to make the required modifications.

Thank you and Kind regards,

Ram

 

Suggest an answer

Log in or Sign up to answer