美文网首页postman
postman Pre-request Script

postman Pre-request Script

作者: 足迹人生2017 | 来源:发表于2017-10-23 09:54 被阅读44次
    
    #### pm.globals
    //检测全局变量是否包含某个变量---返回布尔值
    pm.globals.has(variableName:String)
    //获取全局变量中的某个值
    pm.globals.get(variableName:String)
    //为某个全局变量设置值
    pm.globals.set(variableName:String, variableValue:String)
    //销毁某个全局变量
    pm.globals.unset(variableName:String)
    //清除全局变量
    pm.globals.clear()
    //将全局变量以一个对象的方式全部输出
    pm.globals.toObject():function → Object
    
    
    #### pm.environment
    //检测环境变量是否包含某个变量---返回布尔值
    pm.environment.has(variableName:String)
    //获取环境变量中的某个值
    pm.environment.get(variableName:String)
    //为某个环境变量设置值
    pm.environment.set(variableName:String, variableValue:String)
    //销毁某个环境变量
    pm.environment.unset(variableName:String)
    //清除环境变量
    pm.environment.clear()
    //将环境变量以一个对象的方式全部输出
    pm.environment.toObject():function → Object
    
    
    #### pm.request
    //获取当前发起请求的url
    pm.request.url
    //以数组的方式返回当前请求中的header信息
    pm.request.headers
    
    #### pm.sendRequest
    // example with a plain string URL
    pm.sendRequest('https://postman-echo.com/get', function (err, res) {
        if (err) {
            console.log(err);
        } else {
            pm.environment.set("variable_key", "new_value");
        }
    });
     
    // Example with a full fledged SDK Request
    const echoPostRequest = {
      url: 'https://postman-echo.com/post',
      method: 'POST',
      header: 'headername1:value1',
      body: {
        mode: 'raw',
        raw: JSON.stringify({ key: 'this is json' })
      }
    };
    pm.sendRequest(echoPostRequest, function (err, res) {
      console.log(err ? err : res.json());
    });
     
    // example containing a test ** under the Tests tab only
    pm.sendRequest('https://postman-echo.com/get', function (err, res) {
      if (err) { console.log(err); }
      pm.test('response should be okay to process', function () {
        pm.expect(err).to.equal(null);
        pm.expect(res).to.have.property('code', 200);
        pm.expect(res).to.have.property('status', 'OK');
      });
    });
    
    

    相关文章

      网友评论

        本文标题:postman Pre-request Script

        本文链接:https://www.haomeiwen.com/subject/edeouxtx.html