I'm writing tests for Postman which in general works pretty easily. However, I now want to access some of the data of the request, a query parameter to be exact. You can access the request URL through the "request.url" object which returns a String. Is there an easy way in Postman to parse this URL string to access the query parameter(s)?
The pm.request.url.query.all()
array holds all query params as objects. To get the parameters as a dictionary you can use:
var query = {}; pm.request.url.query.all().forEach((param) => { query[param.key] = param.value});
I have been looking to access the request params for writing tests (in POSTMAN). I ended up parsing the request.url
which is available in POSTMAN.
const paramsString = request.url.split('?')[1]; const eachParamArray = paramsString.split('&'); let params = {}; eachParamArray.forEach((param) => { const key = param.split('=')[0]; const value = param.split('=')[1]; Object.assign(params, {[key]: value}); }); console.log(params); // this is object with request params as key value pairs
edit: Added Github Gist
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With