【问题标题】:How to make a request run after the second request in Postman如何在 Postman 中的第二个请求之后运行请求
【发布时间】:2021-03-01 02:38:20
【问题描述】:

我试图弄清楚如何在第一个请求返回响应后运行第二个请求。我试图在谷歌上搜索,但我对 Postman 有点陌生,我不确定我应该寻找什么. 我试图做类似的事情:

pm.sendRequest(req1, function(err, res) {
   pm.sendRequest(req2, function(err, done)
)});

但是没用

while(documentLength>0)
{
 pm.sendRequest(listDocumentsRequest, function(err, res){
    pm.environment.set('dossierId',res.json().documentsList[index].subDossierId)
    pm.environment.set('documentId',res.json().documentsList[index].documentId)
});
 pm.sendRequest(getDocumentRequest);
    index++;
    documentLength--;
}

所以我尝试发出第一个请求(listDocumentsRequest),然后等到我得到答案,然后运行第二个请求(getDocumentRequest),等到我得到答案然后进入下一个迭代。

你们有什么想法吗?

最好的问候

在寒冷回答后编辑

while(documentLength>0)
{
const interval = setTimeout(() => {}, Number.MAX_SAFE_INTEGER);
function resolvedPromise() {
    return new Promise((resolve, reject) => {
        pm.sendRequest(listDocumentsRequest, (err, res) => {
            if (err) {
                console.log(err);
                reject();
            } else {
                    pm.environment.set('dossierId',res.json().documentsList[index].subDossierId)
                    pm.environment.set('documentId',res.json().documentsList[index].documentId)
                resolve();
            }
        });
    });
}
resolvedPromise()
    .then(pm.request(getDocumentRequest))
    .then(() => clearTimeout(interval))
    .catch(err => {
        console.log(err);
        clearTimeout(interval);
    });
    index++;
    documentLength--;
}

【问题讨论】:

    标签: javascript asynchronous promise postman


    【解决方案1】:
    // Example with a plain string URL
    pm.sendRequest('https://postman-echo.com/get', (error, response) => {
        if (error) {
            console.log(error);
        } else {
            // Example with a plain string URL
            pm.sendRequest('https://postman-echo.com/get/1', (error, response) => {
                if (error) {
                    console.log(error);
                } else {
                    console.log(response);
                }
            });
        }
    });
    

    您可以简单地链接请求您面临的错误是什么?在你的情况下,它看起来像

    while (documentLength > 0) {
        pm.sendRequest(listDocumentsRequest, function (err, res) {
            pm.environment.set('dossierId', res.json().documentsList[index].subDossierId)
            pm.environment.set('documentId', res.json().documentsList[index].documentId)
            pm.sendRequest(getDocumentRequest, function (err, res) {});
            index++;
            documentLength--;
        });
    
    }
    

    【讨论】:

    • 您好,谢谢您的回答,但它仍然无法正常工作,但问题不在于代码本身,可能来自 Postman,因为在我使用此代码运行请求后它崩溃了,但没关系,我在我的情况下找到了一种解决方法。我第一个请求在虚拟请求中的预请求脚本中运行,下一个请求在同一个虚拟请求中的测试选项卡中运行。
    猜你喜欢
    • 1970-01-01
    • 2017-01-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-23
    • 2018-11-02
    • 2019-07-18
    • 2018-06-07
    相关资源
    最近更新 更多