gpt4 book ai didi

postman - 如何在 postman 中从环境变量设置基本授权?

转载 作者:行者123 更新时间:2023-12-02 16:01:08 26 4
gpt4 key购买 nike

我想使用环境变量在 Postman 中设置基本授权。因为我对不同的API调用有不同的授权用户名和密码。

我按照以下方式设置我的 postman :

在“授权”选项卡中:我选择了“无授权”
在标题选项卡中:Key=Authorization Value= Basic{{MyAuthorization}}
在正文选项卡中:

{
"UserName": "{{UserName}}",
"ServiceUrl": "{{ServiceUrl}}"
}

//which set it from the envitonment variable

在预请求选项卡中:

// Require the crypto-js module
var CryptoJS = require("crypto-js");

// Parse the `username` and `password` environment variables
let credsParsed = CryptoJS.enc.Utf8.parse(`${pm.environment.get('admin')}:${pm.environment.get('admin')}`);

// Base64 encoded the parsed value
let credsEncoded = CryptoJS.enc.Base64.stringify(credsParsed);

// Set the valuse as an environment variable and use in the request
pm.environment.set('MyAuthorization', credsEncoded);
console.log(credsEncoded);

在测试选项卡中:

var jsonData = JSON.parse(responseBody);
postman.setEnvironmentVariable("LoginInfoID", jsonData.First.LoginInfoID);

然后我发送了请求并获得了未经授权。

之后,我将身份验证类型设置为带有用户名和密码的基本身份验证它工作正常,我从响应中得到了我想要的。

最佳答案

另一种对我有用的方法:

  1. 设置“用户名”和“密码”的环境变量,并保存
  2. 在请求的“授权”选项卡中,选择“基本身份验证”
  3. 在“用户名”字段中,输入 {{username}}
  4. 在“密码”字段中,点击“显示密码”,然后输入 {{password}}

希望这对其他人有帮助:)

关于postman - 如何在 postman 中从环境变量设置基本授权?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56884095/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com