Javascript 如何在 Postman 中获取请求参数?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/44108015/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 02:20:48  来源:igfitidea点击:

How to get to request parameters in Postman?

javascriptapipostman

提问by Deddiekoel

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)?

我正在为 Postman 编写测试,它通常很容易工作。但是,我现在想访问请求的一些数据,准确地说是查询参数。您可以通过返回字符串的“request.url”对象访问请求 URL。Postman 中是否有一种简单的方法来解析此 URL 字符串以访问查询参数?

采纳答案by Ikbel

If you want to extract the query string in URL encoded format without parsing it. Here is how to do it:

如果您想以 URL 编码格式提取查询字符串而不对其进行解析。这是如何做到的:

pm.request.url.getQueryString() // example output: foo=1&bar=2&baz=3

回答by bbjay

The pm.request.url.query.all()array holds all query params as objects. To get the parameters as a dictionary you can use:

pm.request.url.query.all()数组将所有查询参数保存为对象。要将参数作为字典获取,您可以使用:

var query = {};
pm.request.url.query.all().forEach((param) => { query[param.key] = param.value});

回答by Sudarsan GP

I have been looking to access the request paramsfor writing tests (in POSTMAN). I ended up parsing the request.urlwhich is available in POSTMAN.

我一直在寻找访问用于编写测试的请求参数(在 POSTMAN 中)。我最终解析了request.urlPOSTMAN 中可用的 。

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

POSTMAN CLIENTCONSOLE RESPONSE

邮递员客户控制台响应

edit: Added Github Gist

编辑:添加了Github Gist

回答by HEX

pm.request.url.queryreturns PropertyListof QueryParamobjects. You can get one parameter pm.request.url.query.get()or all pm.request.url.query.all()for example. See PropertyListmethods.

pm.request.url.query回报对propertyListQueryParam对象。例如,您可以获取一个参数pm.request.url.query.get()或所有参数pm.request.url.query.all()。见PropertyList方法。

回答by Dinesh Kumar

I don't think there's any out of box property available in Postman request object for query parameter(s).

我认为 Postman 请求对象中没有任何用于查询参数的开箱即用属性。

Currently four properties are associated with 'Request' object:

目前有四个属性与“请求”对象相关联:

data {object}- this is a dictionary of form data for the request. (request.data[“key”]==”value”) headers {object}- this is a dictionary of headers for the request (request.headers[“key”]==”value”) method {string}- GET/POST/PUT etc.
url {string}- the url for the request.

data {object}- 这是请求的表单数据字典。(request.data[“key”]==”value”) headers {object}- 这是请求的头部字典 (request.headers[“key”]==”value”) 方法 {string}- GET /POST/PUT 等
url {string}- 请求的 url。

Source: https://www.getpostman.com/docs/sandbox

来源:https: //www.getpostman.com/docs/sandbox

回答by A.Joly

have a look in the console doing :

看看在控制台做:

console.log(request);

it'll show you all you can get from request. Then you shall access the different parameters using request., ie. request.name if you want the test name. If you want a particular element in the url, I'm afraid you'll have to use some coding to obtain it (sorry I'm a beginner in javascript)

它会向您展示您可以从请求中获得的所有信息。然后你应该使用 request. 访问不同的参数,即。request.name 如果你想要测试名称。如果您想要 url 中的特定元素,恐怕您必须使用一些编码来获取它(对不起,我是 javascript 初学者)

Hope this helps

希望这可以帮助

Alexandre

亚历山大

回答by Pete

Bit late to the part here, but I've been using the following to get an array of url query params, looping over them and building a key/value pair with those that are

这里的部分有点晚了,但我一直在使用以下内容来获取一组 url 查询参数,循环它们并使用那些

// the message is made up of the order/filter etc params
// params need to be put into alphabetical order
var current_message = '';
var query_params = postman.__execution.request.url.query;
var struct_params = {};

// make a simple struct of key/value pairs
query_params.each(function(param){
    // check if the key is not disabled
    if( !param.disabled ) {
        struct_params[ param.key ] = param.value;
    }
});

so if my url is example.comthen the array is empty and the structure has nothing, {}

所以如果我的 url 是example.com那么数组是空的并且结构什么都没有,{}

if the url is example.com?foo=barthen the array contains

如果 url 是example.com?foo=bar那么数组包含

{
  description: {},
  disabled:false
  key:"foo"
  value:"bar"
}

and my structure ends up being { foo: 'bar' }

我的结构最终是 { foo: 'bar' }

Toggling the checkbox next to the property updates the disabledproperty:

切换属性旁边的复选框会更新disabled属性:

enter image description here

在此处输入图片说明

回答by Pete

Older post, but I've gotten this to work:

较旧的帖子,但我已经开始工作了:

For some reason the debugger sees pm.request.url.query as an array with the items you want, but as soon as you try to get an item from it, its always null. I.e. pm.request.url.query[0] (or .get(0)) will return null, despite the debugger showing it has something at 0.

出于某种原因,调试器将 pm.request.url.query 视为包含您想要的项目的数组,但是一旦您尝试从中获取项目,它始终为空。即 pm.request.url.query[0](或 .get(0))将返回 null,尽管调试器显示它在 0 处有一些东西。

I have no idea why, but for some reason, it is not at index 0, despite the debugger claiming it is. Instead, you need to filter the query first. Such as this:

我不知道为什么,但由于某种原因,它不在索引 0 处,尽管调试器声称它是。相反,您需要先过滤查询。比如这个:

var getParamFromQuery = function (key)
{
    var x = pm.request.url.query;

    var newArr = x.filter(function(item){
        return item != null && item.key == key;
    });

    return newArr[0];
};

var getValueFromQuery = function (key)
{
    return getParamFromQuery(key).value;  
};

var paxid = getValueFromQuery("paxid");

getParamFromQuery returns the parameter with the fields for key, value and disabled. getValueFromQuery returns just the value.

getParamFromQuery 返回包含键、值和禁用字段的参数。getValueFromQuery 仅返回值。