邮递员通过脚本发送带有正文的http请求

ric*_*ard 6 postman

我使用邮递员6.0发送http请求。为了发送请求,我使用请求前脚本获取令牌并将其放入环境中,以便在后续请求中使用它。以下脚本无效,因为未发送正文。有什么事吗?

const getTaxAccessToken={
  url: 'http://dev.xxx.com:4001/api/v1/portal/account/tax-login',
  method: "post",
  body: {
      'loginIdentity': 'admic',
      'password': 'abc123'
  },
  header: {
      'Content-Type': 'application/json'
  }
};
pm.sendRequest(getTaxAccessToken, function (err, response) {
  console.log("get accesstoken");
  console.log(response.access_Token);
  pm.environment.set("taxAccessToken", response.access_Token);
});
Run Code Online (Sandbox Code Playgroud)

Mus*_*tor 14

对于邮递员 >= v8.3.0

\n

Postman v8.3.0update()引入了该方法,允许您直接从预请求脚本设置请求正文。

\n

对于您的用例,您可以简单地使用:

\n
pm.request.body.update({\n    mode: \'raw\',\n    raw: JSON.stringify({\'loginIdentity\': \'admic\', \'password\': \'abc123\'})\n});\n
Run Code Online (Sandbox Code Playgroud)\n

甚至更短:

\n
pm.request.body.update(JSON.stringify({\'loginIdentity\': \'admic\', \'password\': \'abc123\'}));\n
Run Code Online (Sandbox Code Playgroud)\n

字符串、表单数据、urlencoded 和其他内容类型

\n

由于标题不是专门针对 JSON 请求体定制的,我想我应该添加一些示例来说明如何处理其他数据,因为许多人在 Google 上搜索时可能会找到此页面,并遇到其他内容类型的问题。

\n

生的

\n

rawPostman 需要 a string,因此您可以传输任何可以表示为 a 的string内容,例如纯文本、HTML、XML、JSON 等。

\n
// plain text\npm.request.body.update(`Hello World!`);\n// HTML\npm.request.body.update(`<html>...</html>`);\n// XML\npm.request.body.update(`<?xml version="1.0" encoding="UTF-8"?>...`);\n// JSON\npm.request.body.update(JSON.stringify({ key: `value` }));\n
Run Code Online (Sandbox Code Playgroud)\n

URL编码

\n
pm.request.body.update({\n    mode: "urlencoded",\n    urlencoded: [{\n        key: "key",\n        value: "value with spaces and special chars ?/ and umlaute \xc3\xb6\xc3\xbc\xc3\xa4"\n    }]\n});\n
Run Code Online (Sandbox Code Playgroud)\n

表单数据

\n
pm.request.body.update({\n    mode: "formdata",\n    formdata: [{\n        key: "key",\n        value: "value with spaces and special chars ?/ and umlaute \xc3\xb6\xc3\xbc\xc3\xa4"\n    }]\n});\n
Run Code Online (Sandbox Code Playgroud)\n

GraphQL

\n
pm.request.body.update({\n    mode: \'graphql\',\n    graphql: {\n        query: `\n            query {\n                hero {\n                    name\n                    friends {\n                        name\n                    }\n                }\n            }`\n    }\n});\n
Run Code Online (Sandbox Code Playgroud)\n
\n

基于GraphQL 字段教程的示例。

\n
\n

来自本地文件系统的文件作为表单数据

\n
pm.request.body.update({\n    mode: "formdata",\n    formdata: [\n        {\n            key: "file",  // does not need to be "file"\n            type: "file", // MUST be "file"\n            src: "/C:/Users/MyUser/Documents/myFile.zip"\n        }\n    ]\n})\n
Run Code Online (Sandbox Code Playgroud)\n

请注意:这仅适用于当前工作目录中的文件。Form param \'file\', file load error: PPERM: insecure file access outside working directory否则,您将在 Postman 控制台中收到类似这样的错误。

\n

当您转到 时,您可以看到您的工作目录在哪里Settings | General | Working Directory。还有一个选项Allow reading files outside working directory,您可以启用从任何​​地方读取文件,但请注意,这可能允许其他人从您的计算机窃取数据,例如当您执行不受信任的集合时。

\n

Postman 工作目录设置

\n


小智 7

尝试这个。

  body: {
     mode: 'raw',
     raw: JSON.stringify({'loginIdentity': 'admic', 'password': 'abc123'})
  }
Run Code Online (Sandbox Code Playgroud)


Gin*_*pin 6

如果请求需要是类型application/x-www-form-urlencoded

const options = {
  url:  'http://some/url', 
  method: 'POST',
  header: {
    'Accept': '*/*',
    'Content-Type': 'application/x-www-form-urlencoded',
  },
  body: {
    mode: 'urlencoded',
    urlencoded : [
      { key: 'loginIdentity', value: 'admic'},
      { key: 'password', value: 'abc123'},
    ]
  }
};

pm.sendRequest(options, function (err, res) {
  // ...
});
Run Code Online (Sandbox Code Playgroud)

  • 这就是我一直在寻找的!谢谢。我不确定发送请求的不同模式是什么。 (4认同)