Skip to content

Instantly share code, notes, and snippets.

@Willshaw
Created June 15, 2021 09:59
Show Gist options
  • Save Willshaw/ac7e034f022ca00e6bbb7da24b8d2359 to your computer and use it in GitHub Desktop.
Save Willshaw/ac7e034f022ca00e6bbb7da24b8d2359 to your computer and use it in GitHub Desktop.
Pre-request script for Postman calling api.comcar.co.uk/v1/
// build the hash
var current_timestamp = ((new Date()).getTime() / 1000).toFixed();
postman.setEnvironmentVariable('this_timestamp', current_timestamp );
postman.setEnvironmentVariable('this_nonce', Math.floor( Math.random() * 10000 ));
// the message is made up of the order/filter etc params
// params need to be put into alphabetical order
var current_message = '';
var query_params = postman.__execution.request.url.query;
var struct_params = {};
// make a simple struct of key/value pairs
query_params.each(function(param){
// check if the key is not disabled
if( !param.disabled ) {
struct_params[ param.key ] = param.value;
}
});
// make an ordered array of key names
var arr_param_names = Object.keys( struct_params ).sort();
// add the params in order to the message
arr_param_names.each(function(param){
// get the value, blank if it's null
var val = struct_params[ param ]||'';
// if the value is not blank, add it to the message
if( val !== '' ) {
current_message+=param+'='+val;
}
});
var combined_data = environment.api_key
+ current_message
+ environment.api_secret
+ environment.this_nonce
+ environment.this_timestamp
console.log( combined_data )
// make lower case
combined_data = combined_data.toLowerCase();
generated_hash = CryptoJS.SHA256( combined_data );
postman.setEnvironmentVariable('generated_hash', generated_hash);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment