【问题标题】:Postman Get value of url request parameter to do a testPostman 获取url请求参数的值做一个测试
【发布时间】:2021-09-06 11:16:03
【问题描述】:

我正在尝试从获取请求参数中获取 url。

如何从环境变量构造请求 URL

在预请求脚本中,urlwfsservice 和 orderid 是从环境变量中设置的

{{urlwfsservice}}/v1/merchantorders/{{orderId}}/BoardingActivities.updateMerchantToHub.REQUEST/details/

使用时

var urlnew =request.url;
console.log(request.url);

我将此输出作为变量名而不是实际值或 url 得到

{{urlwfsservice}}/v1/merchantorders/{{orderId}}/BoardingActivities.updateMerchantToHub.REQUEST/details/

我怎样才能得到如下 simpleurl 的输出?

var simpleurl = “https://dev-someweburl.com/v1/merchantorders/ZN2aB/BoardingActivities.updateMerchantToHub.REQUEST/details/”;

完整的预请求脚本代码

// how to Construct request URL from environment variables
console.log("logging url");
var urlnew =request.url;
console.log(urlnew);
//var url = "https://dev-someweburl.com/v1/merchantorders/ZN2aB/BoardingActivities.updateMerchantToHub.REQUEST/details/";
var retryDelay = 200;
var retryLimit = 5;

function isProcessingComplete(retryCount) {
    pm.sendRequest(urlnew, function (err, response) {
        if(err) {
            // hmmm. Should I keep trying or fail this run? Just log it for now.
            console.log(err);
        } else {
            // I could also check for response.json().results.length > 0, but that
            // would omit SUCCESS with empty results which may be valid
            if(response.json().auditRecords.length === 0) {
                if (retryCount < retryLimit) {
                    console.log('Job is still PENDING. Retrying in ' + retryDelay + 'ms');
                    setTimeout(function() {
                        isProcessingComplete(++retryCount);
                    }, retryDelay);
                } else {
                    console.log('Retry limit reached, giving up.');
                    postman.setNextRequest(null);
                }
            }
        }
    });
}

isProcessingComplete(1);

【问题讨论】:

    标签: javascript postman postman-pre-request-script


    【解决方案1】:
    console.log(pm.variables.replaceIn(pm.request.url.toString()))
    

    您可以使用 replaceIn 方法将变量替换为其实际值。也可以使用 pm。因为 pm 是 postman 中的新 api 或语法

    【讨论】:

    • “toString() 方法返回一个代表对象的字符串。”永远不会猜到我需要 toString() 根据路径是否包含特定单词有条件地添加查询参数。谢谢,这也回答了我的问题!
    猜你喜欢
    • 2023-04-03
    • 1970-01-01
    • 1970-01-01
    • 2017-10-21
    • 2015-09-13
    • 2018-06-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多