Forum Discussion
I moved to using Powershell. I believe the problem is with the body being explicitly RAW and JSON. I think it has to be RAW or maybe TEXT format to allow variable values to be referenced correctly and then converted to JSON via pre-request script but have not had a chance to test again.
5 hours ago, JonR said:I have what seems like a similar issue with a get operation getting a device by ID. When I set the ID manually in the request it returns a single device as expected. When I set the ID with an environment variable authentication fails with a 1401 code returned. The headers and request path look identical in both cases. Was there a resolution found for the original post?
Working request with ID 5 set statically in the request:
{{url}}/device/devices/5 sends: GET https://(company).logicmonitor.com/santaba/rest/device/devices/5
Broken request with id variable set to 5:
{{url}}/device/devices/{{id}} sends: GET https://(company).logicmonitor.com/santaba/rest/device/devices/5
I tried modifying the pre-request script as well and had the exact same results. Script modification:
var id = pm.environment.get('id'); var request_vars = (http_verb == 'GET'||http_verb == 'DELETE') ? http_verb + epoch + resource_path + id: http_verb + epoch + request.data + resource_path;
The following works for me (specific to JonR's request)
Pre-request script at the collection level:
// Get API credentials from environment variables
var api_id = pm.environment.get('api_id');
var api_key = pm.environment.get('api_key');
var dvId = pm.environment.get('dvId');
var deviceId = pm.environment.get('deviceId')
var dsId = pm.environment.get('dsId');
var datasourceName = pm.environment.get('datasourceName');
var instId = pm.environment.get('instId');
// Get the HTTP method from the request
var http_verb = request.method;
// Extract the resource path from the request URL
var resource_path = request.url.replace(/(^{{url}})([^\?]+)(\?.*)?/, '$2');
var resource_path = resource_path.replace("{{dvId}}", dvId);
var resource_path = resource_path.replace("{{dsId}}", dsId);
var resource_path = resource_path.replace("{{datasourceName}}", datasourceName);
var resource_path = resource_path.replace("{{instId}}", instId);
var resource_path = resource_path.replace("{{deviceId}}", deviceId);
console.log('resource_path: ' + resource_path)
// Get the current time in epoch format
var epoch = (new Date()).getTime();
// If the request includes a payload, included it in the request variables
var request_vars = (http_verb == 'GET'||http_verb == 'DELETE') ?
http_verb + epoch + resource_path : http_verb + epoch + request.data + resource_path;
// Generate the signature and build the Auth header
var signature = btoa(CryptoJS.HmacSHA256(request_vars,api_key).toString());
var auth = "LMv1 " + api_id + ":" + signature + ":" + epoch;
// Write the Auth header to the environment variable
pm.environment.set('auth', auth);
Environment variables (pre-existing):
api_id, api_key, auth, url, dvId, deviceId, dsld, datasourceName, instId (Current Value of dvId is set to a real device ID)
Get request:
{{url}}/device/devices/{{dvId}}
Authorization tab, type is inherit.
Headers tab key:Authorization, value:{{auth}} (also a good idea to set X-Version to 1 or 3 depending on what type of request for future reference. This works without it.)
Pre-request script should have no content (if you are changing device id here, the request will fail as this runs after the collection level pre-request script but before the request is sent).
Related Content
- 2 months ago