在POSTMAN中,我如何获得响应标题项的子字符串? [英] In POSTMAN how do i get substring of response header item?

查看:478
本文介绍了在POSTMAN中,我如何获得响应标题项的子字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用邮递员来获取响应标头值,如下所示:

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

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

我可以通过 console.log(data)很容易。

但是,我真正想要的是 bbb。所以我需要一些 substring()函数类型。显然,数据不是JavaScript字符串类型,因为例如 data.substring(10)始终返回null。

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?

是否存在解释此问题的邮递员API文档?

If any postman API doc existing that explains this?

推荐答案

最初的想法-我需要位置标头的特定部分(例如OP),但我还必须从该特定部分获得特定值。
我的标题看起来像这样

Some initial thought - I needed a specific part of the "Location" header like the OP, but I had to also get a specific value from that specific part. My header would look something like this

https://example.com?code_challenge_method=S256&redirect_uri=https://example.com ; state = vi8qPxcvv7I& nonce = uq95j99qBCGgJvrHjGoFtJiBoo

我需要将 state值作为变量传递给下一个请求

And I need the "state" value to pass on to the next request as a variable

var location_header = pm.response.headers.get("Location");
var attributes = location_header.split('&');

console.log(attributes);

var len = attributes.length;
var state_attribute_value = ""
var j = 0;
for (var i = 0; i < len; i++) {
    attribute_key = attributes[i].split('=')[0];
    if (attribute_key == "state") {
        state_attribute_value = attributes[i].split('=')[1];
    }
    j = j + 1;
}
console.log(state_attribute_value);
pm.environment.set("state", state_attribute_value);

在这里您可能会明白, split是为您提供一些值数组的选择。
如果要拆分的文本始终具有相同的数组长度,则应该很容易捕获正确的数字

Might you get the point here, "split" is the choice to give you some array of values. If the text you are splitting is always giving the same array length it should be easy to catch the correct number

这篇关于在POSTMAN中,我如何获得响应标题项的子字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆