Many thanks.
I have set up the following test using static headers, then converted to scripts in LM. With headers set as static, I can run Step 1, copy the token it generates, paste into the Authorization header in Step 2, and it works, but as the token is only valid for 15 mins, that isnt a workable solution ;)
Step 1 Request Script...
import static com.logicmonitor.service.groovyapi.PostDataType.*;
import static com.logicmonitor.service.groovyapi.StatusCode.*;
import com.logicmonitor.service.groovyapi.StatusCode;
import com.logicmonitor.service.groovyapi.AuthType;
import com.logicmonitor.service.groovyapi.LMRequest;
LMRequest request = new LMRequest();
request.useHttp1_1()
.followRedirect(true)
.needFullpageLoad(false)
.addHeader('''sbsbrand''', '''<CUSTOMER>''')
.addHeader('''sbsreference''', '''<REFERENCE>''')
.addHeader('''sbslanguage''', '''EN''')
.addHeader('''sbschannel''', '''broker''')
.addHeader('''Content-Type''', new String('''YXBwbGljYXRpb24vanNvbg=='''.decodeBase64()))
.post(JSON, new String('''//<Encrypted post data>//'''.decodeBase64()));
return LMHttpClient.request(request);
---
With a response script of
import static com.logicmonitor.service.groovyapi.StatusCode.*;
import com.logicmonitor.service.groovyapi.StatusCode;
StatusCode status = STATUS_OK;
String body = LMResponse.getBody()
import groovy.json.JsonSlurper
def jsonSlurper = new JsonSlurper()
def object = jsonSlurper.parseText(body)
LMResponse.setContext("token", object.token);
int[] codes = [200];
status = STATUS_MISMATCH;
for (int code : codes) {
status = LMResponse.statusMatch(code);
if (status.isOK()) {
break;
}
}
if (!status.isOK()) {
return status;
}
return status;
This generates output such as
Response Content:
{"exists":false,"token":"<token - 367 characters ! >"}
it is this token I need to pass to the second step and set with header set to ‘Authorization: Bearer <token>’
Step 2 Request script (a GET with headers set)
is
import static com.logicmonitor.service.groovyapi.PostDataType.*;
import static com.logicmonitor.service.groovyapi.StatusCode.*;
import com.logicmonitor.service.groovyapi.StatusCode;
import com.logicmonitor.service.groovyapi.AuthType;
import com.logicmonitor.service.groovyapi.LMRequest;
LMRequest request = new LMRequest();
request.useHttp1_1()
.followRedirect(true)
.needFullpageLoad(false)
.addHeader('''Authorization''', 'Bearer ' + token)
.addHeader('''sbsbrand''', '''<CUSTOMER>''')
.addHeader('''sbsreference''', '''<reference>''')
.addHeader('''sbslanguage''', '''EN''')
.addHeader('''sbschannel''', '''broker''')
.addHeader('''Content-Type''', new String('''YXBwbGljYXRpb24vanNvbg=='''.decodeBase64()))
.get();
return LMHttpClient.request(request);