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
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
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
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.JsonOutputimport groovyx.net.http.ContentTypeimport groovyx.net.http.HttpResponseDecoratorimport groovyx.net.http.RESTClientfinal 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
It looks like you're new here. Sign in or register to get started.