且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在POSTMAN中,我如何获得响应标题项的子字符串?

更新时间:2023-11-06 23:41:40

最初的想法-我需要位置标头的特定部分(例如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