-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add pre-request script for LM API authentication in Postman
- Loading branch information
Showing
1 changed file
with
25 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
/* | ||
Script to contruct the auth header for the LM API | ||
Should be added within Postman as a pre-request script | ||
LM community post with steps: | ||
https://community.logicmonitor.com/discussions/product-discussions/accessing-the-logicmonitor-rest-api-with-postman-and-lmv1-api-token-authenticati/5143?topicRepliesSort=postTimeDesc | ||
Note: this script is not needed if using bearer token auth | ||
*/ | ||
|
||
var api_id = pm.environment.get("api_id"); | ||
var api_key = pm.environment.get("api_key"); | ||
|
||
var http_verb = request.method; | ||
var resource_path = request.url.replace(/(^{{url}})([^\?]+)(\?.*)?/, "$2"); | ||
var epoch = new Date().getTime(); | ||
|
||
var request_vars = | ||
http_verb == "GET" || http_verb == "DELETE" | ||
? http_verb + epoch + resource_path | ||
: http_verb + epoch + request.data + resource_path; | ||
|
||
var signature = btoa(CryptoJS.HmacSHA256(request_vars, api_key).toString()); | ||
var auth = "LMv1 " + api_id + ":" + signature + ":" + epoch; | ||
|
||
pm.environment.set("auth", auth); |