Multi Step website test with variable
We have a website test on a site that uses session based tokens, whcih are passed by the authentication process.
I can curl the website and get a token, and then paste the token as a bearer token in another curl, but can’t figure out:-
- how to do this with a website test
or - how to build a new datasource to do this.
The token expires after 15 minutes, so setting up as a persistant value in the headers for the webtest doesnt work.
Can anyone help?
Tim_OShea
Posted 3 years ago·Last reply 3 years ago
13 comments
LM User
·3 years agoSo this line:
Saved the token value for use in the next step, though i’m not sure if it’s a method of the LMResponse object. I thought you could just do `setContext(“token”,object.token);`
Anyway, you’d need a `token = getContext(“token”)` in your step 2 request script to get the token into the script, then yes, you can set that token as your Auth header.
Also, the content-type header can probably be as simple as:
Putting it in encoded threw me off.
Tim_OShea
OP3 years agoMany 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);
LM User
·3 years agoThis is why I don’t pay for premier support. The stuff I need help with is the stuff they run away from like chicken pox.
Mind sharing your code (redact any private data).
Tim_OShea
OP3 years agohi all, still struggling with this. I submitted a call with LM support, but reached the point where i was told it is a custom script so they couldn’t help further.
I tried following the video and link provided, but still get an error when i try to pass the token as a variable between the 2 steps.
I have scripted 2 curls on the servers, and used a datasource to call the script, as a workaround, but the completist in me wants to get the web check working if it is possible.
LM User
·3 years agoWhoops, sorry, forgot to tell you to import the json library first. I edited the above code to include the import.
Tim_OShea
OP3 years agothat gives me a ‘No such property: jsonSlurper for class’
I changed to JsonSlurper, thinking it muight be a typo, and this returned a different error -
No signature or method: static.groovy.jason.JsonSlurper.parseTest() is applicable for argument type
LM User
·3 years agoTim_OShea
OP3 years agoGreat, many thanks..
My website is returning the token in json in the form:
{"exists":false,"token":"<token>}
How do I get just the token into the variable?
I added
String body = LMResponse.getBody()
result= (body+~ /token"\s+valuie="(\d+)"/)[0][1];
into the response script, but that errors - Script Error - ScriptExecutingFailedExpression: begin 1, end 2, length 1
LM User
·3 years agoYep, that’s it. Should help you out.
Joseph Brett
·3 years agoHey @Tim OShea
This is the video that @Stuart Weenig was referring too:
https://logicmonitor.wistia.com/medias/ujv0u10xv6Tim_OShea
OP3 years agoHi,
Sorry if I am asking a dumb question, but where would I find the video?
I am more of a sysadmin than a developer, so have limited coding and need a bit of a dummies guide.
LM User
·3 years agoThere’s a video the LM training team has that shows how to do it. @Victoria Verhulst made it a long time ago, but it’s still valid.
Joseph Brett
·3 years agoHey @Tim OShea
So, this can be accomplished using an internal web check in your portal. It will require the use of groovy coding to accomplish so you can pull that token and then pass it to the next step in the check.
This doc does go over that process:
https://www.logicmonitor.com/support/services/adding-managing-services/executing-internal-web-checks-via-groovy-scripts
I would also recommend reaching out to the LogicMonitor support team should you have any questions as although we cannot craft those custom scripts for you, we have dealt with this quite a bit and will likely be able to get you up an running!