Postman advanced articles (eleven) - use pm object access interface request in the script (pm.request.*)

  • In the previous article, I introduced two scripts in postman - pre-request script or test script, and the pm object is used in both scripts. ( pre-request script detailed introduction , Test script detailed introduction )
  • The pm object is very important in postman scripts, and it is also a very commonly used method.
  • This article mainly introduces how to use the pm object to operate interface requests.

1. Introduction to pm objects

  • The pm object is a JavaScript API provided by Postman that can be used in request scripts.
  • The pm object provides access to request and response data and variables, and the pm object can be used to operate on request data or response data.

2. Use PM objects to access and manipulate request parameters (pm.request.*)

  • The pm.request object can access the request data in the script.
  • For pre-request scripts, the pm.request object accesses the request that is about to be run, and for test scripts, the request that has already been run.
  • If you use pm.request in the pre-request script, you can add, delete, or modify request parameters before requesting.
  • Note: Using pm.request to add, delete, or modify request parameters will not modify the original data of the interface, and will only take effect when the interface is running this time

The following content involves the following knowledge points:
( pre-request script detailed introduction , console console detailed introduction )

(1) The URL requested by the interface

  • Interface URL
pm.request.url //接口URL
  • Access and operate the interface URL through the URL object:

    • Get the host in the URL:
      pm.request.url.getHost()
      
    • Return request path:
      pm.request.url.getPath()
      
    • Returns the full path in the request, including the query string:
      pm.request.url.getPath()
      
    • Returns the query parameters in string format:
      pm.request.url.getQueryString()
      
    • Return host and port, separated by ":":
      pm.request.url.getRemote()
      
    • Pass in key-value pairs and add query parameters:
      pm.request.url.addQueryParams({
              
              key: "variable_key",value: "variable_value"})  
      //运行后添加的查询参数为 variable_key=variable_value
      
    • Delete specified query parameters:
      //删除单个查询参数
      pm.request.url.removeQueryParams(“variable_key”)  //运行后删除查询参数 variable_key
      
      //删除多个查询参数
      pm.request.url.removeQueryParams([“variable_key”,“variable_key2”]) //运行后删除查询参数 variable_key和variable_key2
      
    • Setting the URL will only take effect in this run:
      pm.request.url.update("http://postman-echo.com/post")
      //运行后添加的URL为 http://postman-echo.com/post
      
  • Use the above method in the pre-request script to view the effect after running through the console:
    insert image description here

(2) Interface request header

  • interface request header
pm.request.headers //接口请求头列表
  • Access and operate on the interface request header:
    • Pass in key-value pairs and add request header parameters:
      pm.request.headers.add({
              
              key: "variable_key",value: "variable_value"})  
      //运行后添加请求头参数为 variable_key=variable_value
      
    • Return all request header parameters:
      pm.request.headers.all()
      
    • Get the corresponding value by key:
      pm.request.headers.get("variable_key")  
      
    • Determine whether the parameter exists by key:
      pm.request.headers.has("variable_key")
      
    • Clear request header parameters:
      pm.request.headers.clear() 
      
    • Add a request header parameter, or modify an existing request header parameter:
      pm.request.headers.upsert({
              
              key: "variable_key",value: "variable_value"}) 
      
    • Remove the specified request header parameters:
      pm.request.headers.remove("variable_key")
      
    • The size of the request header list in addition to the standard request header parameters:
      pm.request.headers.contentSize()
      
    • The length of the request header list:
      pm.request.headers.count()
      
  • Use the above method in the pre-request script to view the effect after running through the console
  • There are many methods, so I won’t demonstrate them one by one. You can refer to the example and actually run it in postman.
    insert image description here
  • Most of the time using the appeal method is sufficient. In addition, you can also use the built-in method to operate on the request header parameters. If you are interested, you can try:
    pm.request.headers.each(function(i) {
          
          console.log(i)}) //返回请求头列表里每一个参数,并可以通过函数处理参数
    pm.request.headers.filter({
          
          value: "value_1"})//过滤器,根据条件返回对应的请求头参数。此方法的返回结果为数组,只要符合条件的参数都会返回。
    pm.request.headers.find({
          
          value: "value_1"}) //查找请求头参数,返回符合条件的参数。此方法只会返回第一个匹配的结果。
    pm.request.headers.idx(0) //传入整数,返回请求头参数列表中对应位置的参数
    pm.request.headers.insert({
          
          key: "header_3",value: "value_3"},"header_1") //插入一个请求头参数,默认插入在末尾,如果第二个参数传入请求头参数的key,也可以插入这个请求头参数之前。
    pm.request.headers.insertAfter({
          
          key: "header_3",value: "value_3"},"header_1") //与insert类似,不一定的是这个方法是插入在指定参数之后
    pm.request.headers.prepend({
          
          key: "header_3",value: "value_3"})//在请求头列表的第一位添加请求头参数
    pm.request.headers.toObject() //以Object形式返回请求头列表
    pm.request.headers.toString() //以字符串形式返回请求头列表
    

(3) Interface request body

For the introduction of each type of request body, please refer to the previous article in the column:
( Request body type introduction and configuration )

  • interface request header
pm.request.body //接口请求头列表
  • The method can be used directly for the request body:
    • Whether the request body is empty:
      pm.request.body.isEmpty()
      
    • Return request body in json format:
      pm.request.body.toJSON()
      
  • But to operate a specific parameter, you need to access the properties of pm.request.body:
    • The mode of the request body:
      pm.request.body.mode
      
    • Request body url encoding mode:
      pm.request.body.urlencoded
      
    • Request body form mode:
      pm.request.body.formdata
      
    • Request body file:
      pm.request.body.file 
      
    • Request body graphic language mode:
      pm.request.body.graphql
      
    • Request body source mode:
      pm.request.body.raw 
      
  • Here we take the commonly used form mode (formdata) as an example:
  • If the interface request parameter setting is configured in form mode, you can use the following methods to manipulate parameters:
    insert image description here
    • Number of request body parameters:
      pm.request.body.formdata.count()
      
    • New parameters:
      pm.request.body.formdata.add({
              
              key: "key",value: "value"})
      
    • Add parameters at the end of the request body:
      pm.request.body.formdata.append({
              
              key: "key",value: "value"}) 
      
    • Return all body parameters:
      pm.request.body.formdata.all()
      
    • Return the parameter list as JSON:
      pm.request.body.formdata.toJSON() 
      
    • Clear request body:
      pm.request.body.formdata.clear()
      
    • Get the parameter value:
      pm.request.body.formdata.get("key")
      
    • Determine whether the parameter exists:
      pm.request.body.formdata.has("key")
      
    • Remove specified parameters:
      pm.request.body.formdata.remove("key")
      
    • Add a request header parameter, or modify an existing request header parameter:
      pm.request.body.formdata.upsert({
              
              key: "key",value: "value"}) 
      
    • Similar to the request header list, the request body can also only use the built-in method. If you are interested, you can try it:
      pm.request.body.formdata.each(function(i) {
              
              console.log(i)})
      console.log("过滤器:",pm.request.body.formdata.filter({
              
              value: "value_"}))//过滤器,根据条件返回对应的参数。此方法的返回结果为数组,只要符合条件的参数都会返回。
      console.log("查找:",pm.request.body.formdata.find({
              
              value: "value_"})) //查找参数,返回符合条件的参数。此方法只会返回第一个匹配的结果。
      console.log("获取排在第x位的参数:",pm.request.body.formdata.idx(0)) //传入整数,返回请求头参数列表中对应位置的参数
      pm.request.body.formdata.insert({
              
              key: "body_3",value: "body_3"},"body_") //插入一个参数,默认插入在末尾,如果第二个参数传入参数的key,也可以插入这个请求头参数之前。
      pm.request.body.formdata.insertAfter({
              
              key: "body_4",value: "body_4"},"body_3") //与insert类似,不一定的是这个方法是插入在指定参数之后
      pm.request.body.formdata.prepend({
              
              key: "body_0",value: "body_0"})//在参数列表的第一位添加请求头参数
      console.log("参数列表(Object):",pm.request.body.formdata.toObject()) //以Object形式返回参数列表
      
  • The URL encoding mode (urlencoded) is similar to the form mode. When operating parameters, you only need to replace pm.request.body.formdata with pm.request.body.urlencoded.
  • Parameters in source mode (raw) can be manipulated using the built-in string methods:
    pm.request.body.raw.toString() //以字符串的形式返回参数
    pm.request.body.raw.toUpperCase() //将字符串中的所有字母字符转换为大写。
    pm.request.body.raw.trim() //从字符串中删除前导空格和尾随空格以及行终止符。
    pm.request.body.raw.toLowerCase() //将字符串中的所有字母字符转换为小写。
    pm.request.body.raw.split() //使用指定的分隔符将字符串拆分为子字符串,并将其作为数组返回。
    pm.request.body.raw.slice() //返回字符串的一部分。
    pm.request.body.raw.search() //在正则表达式搜索中查找第一个子字符串匹配项。
    pm.request.body.raw.replace() //使用支持字符串内替换的对象替换字符串中的文本。
    pm.request.body.raw.match() //匹配支持匹配的字符串或对象,并返回数组包含该搜索的结果,如果未找到匹配项,则为空。
    pm.request.body.raw.length //长度
    pm.request.body.raw.indexOf() //在字符串中搜索关键字,返回关键字第一次出现的位置。
    

(4) Interface request method

pm.request.method //接口请求头列表
  • The request header method can be called directly, and the return value is a string, or only the built-in string method can be used, which will not be described in detail here.

3. Example: Modify the request parameter mode in the script (formdata is changed to raw)

For the actual application of pm.request, you can also refer to another article in the column:
Actual combat: pre-request script encryption interface request parameters (AES, MD5)

  • If you want to modify the parameter mode in the script, you need to modify the value of pm.request.body.mode
//设置请求参数模式
pm.request.body.mode = "raw"
  • To give an example, interface parameters are set in formdata mode
    insert image description here
  • Set the mode to "raw" in the pre-request script
pm.request.body.mode= "raw"//body的模式
pm.request.body.raw = "raw_value,raw_value2" //设置raw参数
  • After running the interface, you can see that the interface parameters are finally requested in "raw" mode.
    insert image description here

Appendix: postman series article catalog

This article is a companion article:
Postman Advanced (12) - Use the pm object to access the interface response data in the script (pm.response.*)

零基础入门接口功能测试教程-目录
—————————————————————————————————
postman系列文章内所使用的示例The file has been uploaded to the official account of my operation [Skills required for software testing]
insert image description here
If necessary, you can click on the article to send the QR code and go to collect it~
insert image description here

Guess you like

Origin blog.csdn.net/weixin_40883833/article/details/127951696