I have below string and want to extract the value of code. I used split function but which runs fine in postman but when i execute same in newman it gives error.
header1=https://debugger.com/ultradebugcode?code=EgxIZAAU3uHVt20pc9qqpv-xNcAWxitpB0vgMiulNLG2hkysukmjR04Fxxnuz9Yb&state=ABC
I want to extract the value of code. which in this case is
EgxIZAAU3uHVt20pc9qqpv-xNcAWxitpB0vgMiulNLG2hkysukmjR04Fxxnuz9Yb
the code i am using is
var str= pm.response.headers.get('header1');
var str1= str.split('code=', 2)[1];
var code= str1.split('&', 2)[0]; // get the code
It worked fine in postman but why newman is giving error here?
You could take a look at the . split() JS method to get that part of the string. This will create a new array of values based on the separator provided. Then you would need to specify the item you want, this will be zero-indexed so 0 would be the first item.
use postman. getResponseHeader in test collection to get header value. If the header value returned from server is as following. The value postman.
Go to the Tests tab on the request page. Now write the tests for validating the GET request: Let us consider an example for writing a test to this GET request, we have to check the response code is 200, and check the response body has the first name “Janet” present. tests[“Validate Status Code”] =responseCode.
This worked for me:
let str = pm.response.headers.get("header1").split("code=")[1]
console.log(str.split("&")[0])
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