javascript 如何在 PostMan 的预请求脚本中计算 md5 哈希?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28992147/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to compute a md5 hash in a pre-request script in PostMan?
提问by Quentin
I have to set a parameter in my request that is a md5 hash of two other parameters. I think a pre-request script can do the job, but I do not know how to compute a md5 in this script. Any idea?
我必须在我的请求中设置一个参数,它是其他两个参数的 md5 哈希值。我认为预请求脚本可以完成这项工作,但我不知道如何在此脚本中计算 md5。任何的想法?
回答by darryn.ten
You can create the following pre-request script provided your parameters are defined environment variables. You would need to tweak this example if they are defined in some other fashion.
如果您的参数是定义的环境变量,您可以创建以下预请求脚本。如果它们以其他方式定义,则需要调整此示例。
// Access your env variables like this
var str_1 = environment.variable_1 + environment.variable_2;
// Or get your request parameters
var str_2 = request.data["foo"] + request.data["bar"];
// Use the CryptoJS
var hash = CryptoJS.MD5(str_1 + str_2).toString();
// Set the new environment variable
postman.setEnvironmentVariable('hash', hash);
CryptoJS works because it's available in Postman (as well as lodash, backbone etc).
CryptoJS 之所以有效,是因为它在 Postman(以及 lodash、backbone 等)中可用。
Accessing environment variables is easy through the environment
object.
通过environment
对象访问环境变量很容易。
Setting environment variables is available through the postman
object.
可以通过postman
对象设置环境变量。
After this pre-request has run you can access the hash
variable using the normal {{hash}}
shorthand.
在此预请求运行后,您可以hash
使用普通{{hash}}
速记访问该变量。
Also, you can read hereabout libraries, variables and properties available in Postman.
此外,您可以在此处阅读 有关 Postman 中可用的库、变量和属性的信息。
回答by Matt
FYI, since Postman 4.6.0+ you can no longer include external libraries with $.getScript calls and promise returns, as jQuery was depreciated in favour of Cheeriofrom 4.6.0.
仅供参考,从 Postman 4.6.0+ 开始,您不能再包含带有 $.getScript 调用和 promise 返回的外部库,因为 jQuery从 4.6.0开始贬值而支持Cheerio。
Therefore if you wish to include a third party library in your pre-request scripts, at present the only way to do so is storing the contents of the file in a environment variable and running eval() in your sript - see why here.
因此,如果您希望在您的预请求脚本中包含第三方库,目前唯一的方法是将文件内容存储在环境变量中并在您的 sript 中运行 eval() -在这里查看原因。
For example:
例如:
eval(postman.getEnvironmentVariable("JSSHA"));
There are however some libraries included with Postman that do not require this workaround, these are listed here.
但是,Postman 中包含的一些库不需要此解决方法,这些在此处列出。