Forum Discussion
I haven't been getting those errors and I'm running 11.2.0 of postman.
Here's my pre-request script:
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,pm.environment.get('api_key')).toString());
var auth = "LMv1 " + pm.environment.get('api_id') + ":" + signature + ":" + epoch;
pm.environment.set('auth', auth);
You have it in the pre-req script not the post-req right?
You should be able to use a bearer token with v3 of the API, so that's another option. I haven't switched yet because mine's working fine.
Yes the script is in the Pre-req section for the collection I set up and my script matches what you've shared.
The script still seems to work, I'm only seeing these warnings if I open up the console. I did do a small recent update it had pending so it probably has something to do with that. I don't know enough to know if these warnings would be because of a change in Postman or a change with Javascript
- mray6 months agoLM Conqueror
Here's an updated version that gets rid of those errors in the console:
var btoa = require('btoa'); var cjs = require('crypto-js'); var api_id = pm.environment.get('api_id'); var api_key = pm.environment.get('api_key'); var http_verb = pm.request.method; var resource_path = pm.request.url.toString().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 + pm.request.data + resource_path; var signature = btoa(cjs.HmacSHA256(request_vars,api_key).toString()); var auth = "LMv1 " + api_id + ":" + signature + ":" + epoch; pm.environment.set('auth', auth);
- Anonymous6 months ago
I can verify this works in 11.2.0. Thanks mray !
- pgordon6 months agoAdvisor
Yes this does work for me, no more warnings.
I appreciate it a lot, thank you!! - pgordon6 months agoAdvisor
I'm still learning so I didn't realize this right away, but this didn't work for POST or PATCH. I was able to figure this part out though!
var request_vars = (http_verb == 'GET'||http_verb == 'DELETE') ? http_verb + epoch + resource_path : http_verb + epoch + pm.request.data + resource_path;
this actually needs to have pm.request.body instead of pm.request.data, so
var request_vars = (http_verb == 'GET'||http_verb == 'DELETE') ? http_verb + epoch + resource_path : http_verb + epoch + pm.request.body + resource_path;
:)
- mray5 months agoLM Conqueror
Oh wow nice catch! I legit did not properly test with a POST 🙃
Related Content
- 2 years ago
- 7 months ago