【问题标题】:Add header to every request in Postman in pre-request script在预请求脚本中为 Postman 中的每个请求添加标头
【发布时间】:2018-12-10 09:58:30
【问题描述】:

我想使用这个预请求脚本自动为我的整个集合中的每个请求添加一个标头:

pm.request.headers.add({
    'key': "myvar",
    'value': pm.environment.get("myvar")    
});

myvar 是一个环境变量。

不幸的是,它不起作用。我错过了什么吗?

【问题讨论】:

    标签: postman


    【解决方案1】:

    这当然有效。松开键和值上的引号

    pm.request.headers.add({
        key: "myvar",
        value: pm.environment.get("myvar")    
    });
    

    【讨论】:

      【解决方案2】:

      对于那些在 postman ~ 7.10.0 上尝试它的人,您可以在预请求脚本中以编程方式将标头添加到请求或集合中(进入集合将向集合内的所有请求添加标头)。

      pm.request.headers.add({ 
          // These keys appears when you set a header by hand. Just for fun they are here
          disabled: false,
          description:{
              content: "DescriptionTest",
              type: "text/plain"
          },
          // Your header, effectively
          key: 'KeyTest', 
          name: 'NameTest', 
          // If you set a variable you can access it
          // HeaderTest here has value="ValueHeaderTest"
          value: pm.collectionVariables.get("HeaderTest")
      });
      

      代码 sn-p 生成器不会显示添加的标头:

      GET /get_info.php HTTP/1.1
      Host: 192.168.15.25:8001
      Content-type: application/json
      User-Agent: PostmanRuntime/7.19.0
      Accept: */*
      Host: 192.168.15.25:8001
      Accept-Encoding: gzip, deflate
      Connection: keep-alive
      

      但邮递员控制台会:

      GET /get_info.php HTTP/1.1
      Content-type: application/json
      KeyTest: ValueHeaderTest
      User-Agent: PostmanRuntime/7.19.0
      Accept: */*
      Host: 192.168.15.25:8001
      Accept-Encoding: gzip, deflate
      Connection: keep-alive
      

      【讨论】:

        【解决方案3】:

        截至Postman v7.0.9,现在可以通过在集合上添加预请求脚本来实现。

        为此,请转到您的收藏,右键单击它,选择编辑,然后转到Pre-request Scripts 选项卡,您可以在其中添加您的 sn-p,即:

        pm.request.headers.add({
          key: 'X-HEADER-TEST',
          value: '1'
        });
        

        【讨论】:

          【解决方案4】:

          我想你可以试试这个方法:

            // Add or update an existing header
          
           pm.request.headers.upsert({
           'key': "myvar",
           'value': pm.environment.get("myvar") 
           });
          

          这已在 Postman 应用程序 (v7.0.9) 中更新。如需更多参考,您可以参考: https://github.com/postmanlabs/postman-app-support/issues/1947

          【讨论】:

          • 不。不工作。标头设置为disabled: true
          【解决方案5】:

          看起来pm.request.headers.add() 目前没有更新正在发送的请求。它已被标记为功能请求:https://github.com/postmanlabs/postman-app-support/issues/4631

          您可能已经知道可以创建预设标题(从“预设”下拉菜单中),以使您更轻松地设置标题。设置下有几个选项可以包含特定的标题。但这些建议不会像您询问的那样自动为整个集合中的每个请求添加标头。

          更新: Postman added support for this in Postman App (v7.0.9)

          【讨论】:

          • 感谢您提及,但我已经在使用预设标题。不过,我不知道我要求的这个功能还没有实现。
          【解决方案6】:

          在登录的测试部分,使用此脚本记住环境中的令牌

          var jsonData = JSON.parse(responseBody);
          
          tests["Body contains result"] = responseBody.has("result");
          
          var result = jsonData.result
          
          tests["result contains user"] = result.user !== null
          var user = result.user
          tests["result contains token"] = result.token !== null
          var token = result.token
          var accessToken = token.accessToken
          var refreshToken = token.refreshToken
          
          postman.setEnvironmentVariable("accessToken", accessToken);
          postman.setEnvironmentVariable("refreshToken", refreshToken);
          

          在每个需要令牌的调用中,在标题部分使用这样的令牌

          Authorization = Bearer {{accessToken}}
          

          【讨论】:

            【解决方案7】:

            这是从这里复制的,但它对我有用

            https://gist.github.com/madebysid/b57985b0649d3407a7aa9de1bd327990

            pm.sendRequest({
                url: "https://mydomain/ers/config/endpoint",
                method: 'GET',
                header: {
                    'content-type': 'application/json',
                    'accept': 'application/json',
                    //'x-site-code': pm.environment.get("x-site-code")
                    'X-CSRF-TOKEN': 'fetch'
                },
                body: {
                    mode: 'raw'//,
                    raw: JSON.stringify({ email: pm.environment.get("email"), password: pm.environment.get("password") })
                }
            }, function (err, res) {
            
                pm.environment.set("X-CSRF-TOKEN", "Bearer " + res.json().token);
            });
            

            【讨论】:

            • 成功了。感谢您将参考链接与您的答案一起提供。
            猜你喜欢
            • 2022-01-16
            • 2018-02-28
            • 2021-09-28
            • 2017-01-25
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2021-05-11
            相关资源
            最近更新 更多