在预请求脚本中添加一个变量来请求URL

问题描述 投票:0回答:1

我在一个集合中有 2 个 Postman 请求,第一个请求应该在第二个请求运行之前运行。 第一个请求是 POST 请求,它会在响应正文中为您提供一个 messageId,

    {
       "messageId": "test-abcdefg",
       "messageStatus": "PUSHED"
    }

第二个请求是 GET 请求,它传递 url 中的 messageId,

https://abcd.com.au/stp/v1/aaa/abcd@account/{{messageId}}

因此,在第二个请求中,我在 url 中添加了一个占位符来传递 messageId。我编写了一个预请求脚本来运行第一个请求并将其值传递给第二个请求。 这是我的预请求脚本,

pm.sendRequest({
    url: 'url',
    method: 'POST',
    header: {
        'content-type': 'application/json'
    },
    body: {
            mode: 'raw',
            raw: [
                    { key: "key1", value: 'abcd' },
                    { key: "key2", value: '123456789' },
                    { key: "key3", value: 1 },
                    { key: "key4", value: 'abcd' }
            ]
    }
}, (err, res) => pm.variables.set("messageId", res.json().messageId));

但是,我收到以下错误,

There was an error in evaluating the Pre-request Script:Error: Unexpected token '<' at 2:1 <!DOCTYPE html> ^

我做错了什么?

提前致谢。

json postman
1个回答
0
投票

我对上述预请求脚本的理解告诉我,您发送的post请求正在向您发送一个错误的HTML页面,这就是为什么它无法解析

res.json()

尝试改变

 raw : JSON.stringify([
                { key: "key1", value: 'abcd' },
                { key: "key2", value: '123456789' },
                { key: "key3", value: 1 },
                { key: "key4", value: 'abcd' }
          ])

如果这不起作用,

我会要求您检查您所在的网址,发送预请求脚本或检查网址所需的正文参数。如果符合要求,请检查 Post API 对标头类型或身份验证的要求。

我希望这有帮助。

© www.soinside.com 2019 - 2024. All rights reserved.