In Postman script transmission request (pm.sendRequest)

The Postman Collection (set) / Folder (sub-folder in the file set) / Request (request) has Tests Pre-request script and scripts two regions, each script may be used before sending a request and the request (based on Javascript implement various operating)
Script collection area

Scripts folder area

Request script area

The results in the face of the interface has a dependency, such as the need to log required or when the previous interface, acquisition parameters, we often need to send it relies request before the request, we can use pm in the Pre-request script. sendRequest achieve

  1. Send GET request
const url = 'http://115.28.108.130:5000/api/user/getToken/?appid=136425';
// 发送get请求
pm.sendRequest(url, function (err, res) {
  console.log(err ? err : res.text());  // 控制台打印请求文本
});

May be blended pm.environment.set (key: value) to the data stored in the environment variable in response to this request for use
Example: use request token and acquires the script before use,
Postman use a script to send get request

  1. Post format transmission request form
//构造一个登录请求
const loginRequest = {
    url: 'http://115.28.108.130:5000/api/user/login/',
    method: "POST",
    body: {
        mode: 'urlencoded',  // 模式为表单url编码模式
        urlencoded: 'name=张三&password=123456'
    }
};

// 发送请求
pm.sendRequest(loginRequest, function (err, res) {
    console.log(err ? err : res.text());
});

Postman output information by clicking on the menu bar -> view-> Show Postman Console, open the console view (first open the console, and then send a request)

Postman using scripts to send form request Post

  1. JSON format transmission request
// 构造一个注册请求
const regRequest = {
  url: 'http://115.28.108.130:5000/api/user/reg/',
  method: 'POST',
  header: 'Content-Type: application/json',  //注意要在Header中声明内容使用的类型
  body: {
    mode: 'raw',  // 使用raw(原始)格式
    raw: JSON.stringify({ name: '小小', password: '123456' }) //要将JSON对象转为文本发送
  }
};

//发送请求
pm.sendRequest(regRequest, function (err, res) {
  console.log(err ? err : res.json());  // 响应为JSON格式可以使用res.json()获取到JSON对象
});

Postman script request to send JSON format Post

  1. XML format transmission request
    transmitted in XML format and transmits almost JSON format, as long as the contents of the specified format and transmitted to the corresponding content
//构造请求
const demoRequest = {
  url: 'http://httpbin.org/post',
  method: 'POST',
  header: 'Content-Type: application/xml',  // 请求头种指定内容格式
  body: {
    mode: 'raw',
    raw: '<xml>hello</xml>'  // 按文本格式发送xml
  }
};

//发送请求
pm.sendRequest(demoRequest, function (err, res) {
  console.log(err ? err : res.json());
});

This article demonstrated Interface - Interface Documentation Portal

For more information, please add add learning of micro letter: lockingfree get

Guess you like

Origin www.cnblogs.com/superhin/p/10984003.html