Tip of the Postman script sends a request

Postman used should know 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 a request ( based on Javascript implement various operations), today to talk about the simple use of this part is how.

Interface inside the Pre-request script and Tests two scripts areas:

Interface set (Collections) inside the Pre-request script and scripts Tests two regions:

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 ' ;
     // send get request 
    pm.sendRequest (URL, function (ERR, RES) { 
      the console.log (ERR ? ERR: res.text ());   // console print request 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,

     

  2. 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菜单栏 ->view-> Show Postman Console, 打开控制台查看(先打开控制台,再发送请求)

     

  3. 发送JSON格式请求
     1 // 构造一个注册请求
     2 const regRequest = {
     3   url: 'http://115.28.108.130:5000/api/user/reg/',
     4   method: 'POST',
     5   header: 'Content-Type: application/json',  //注意要在Header中声明内容使用的类型
     6   body: {
     7     mode: 'raw',  // 使用raw(原始)格式
     8     raw: JSON.stringify({ name: '小小', password: '123456' }) //要将JSON对象转为文本发送
     9   }
    10 };
    11 
    12 //发送请求
    13 pm.sendRequest(regRequest, function (err, res) {
    14   console.log(err ? err : res.json());  // 响应为JSON格式可以使用res.json()获取到JSON对象
    15 });

     

  4. 发送XML格式请求
    发送XML格式和发送JSON格式差不多, 只要指定内容格式并发送相应的内容即可
     1 //构造请求
     2 const demoRequest = {
     3   url: 'http://httpbin.org/post',
     4   method: 'POST',
     5   header: 'Content-Type: application/xml',  // 请求头种指定内容格式
     6   body: {
     7     mode: 'raw',
     8     raw: '<xml>hello</xml>'  // 按文本格式发送xml
     9   }
    10 };
    11 
    12 //发送请求
    13 pm.sendRequest(demoRequest, function (err, res) {
    14   console.log(err ? err : res.json());
    15 });

     

 

 

 

 

Guess you like

Origin www.cnblogs.com/zdd-803/p/11369736.html