Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In POSTMAN how do i get substring of response header item?

Tags:

I am using postman to get response header value like below:

var data = postman.getResponseHeader("Location") . //value is "http://aaa/bbb" for example 

I can print the value via console.log(data) easily.

However, what I really want is "bbb". So I need some substring() type of function. And apparently 'data' is not a javascript string type, because data.substring(10) for example always return null.

Does anyone what i need to do in this case?

If any postman API doc existing that explains this?

like image 556
user1559625 Avatar asked Jan 08 '19 04:01

user1559625


2 Answers

You can set an environment variable in postman. try something like

var data = JSON.parse(postman.getResponseHeader("Location"));
postman.setEnvironmentVariable("dataObj", data.href.substring(10));
like image 56
Bhoomi Avatar answered Oct 11 '22 15:10

Bhoomi


You have the full flexibility of JavaScript at your fingertips here, so just split the String and use the part after the last /:

var data = pm.response.headers.get("Location").split("/").pop());

See W3 school's documentation of split and pop if you need more in depth examples of JavaScript internals.

like image 45
Stefan Haberl Avatar answered Oct 11 '22 15:10

Stefan Haberl