Forum Discussion
Would anyone be able to update the pre-request script? I'm very very new to Postman and using APIs, I've recently started getting warnings that some parts of this script are deprecated when I run it:
Using "request" is deprecated. Use "pm.request" instead.
Using "CryptoJS" is deprecated. Use "require('crypto-js')" instead.
Using "btoa" is deprecated. Use "require('btoa')" instead.
I tried to just start updating it with what it says to use but somehow it's not working completely correctly:
TypeError: pm.request.url.replace is not a function
I'm not familiar enough with this to know if there's a better way to do this instead of what looks like forcing it to not complain about using deprecated functions (that is what those two "requires" are doing yeah?)
Thank you!
- Anonymous6 months ago
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.
- pgordon6 months agoAdvisor
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);
Related Content
- 2 years ago
- 7 months ago