Skip to content

Instantly share code, notes, and snippets.

@angyvolin
Created August 1, 2016 19:26
Show Gist options
  • Select an option

  • Save angyvolin/b53d4f0669d254006f2bf05f07035591 to your computer and use it in GitHub Desktop.

Select an option

Save angyvolin/b53d4f0669d254006f2bf05f07035591 to your computer and use it in GitHub Desktop.
Use Postman pre-request script for custom authentication

Custom auth via Postman's Pre-request Script.

Usage:

  • add request header, for instance X-Api-Token with value {{secret}}

  • provide a pre-request script which builds the secret token and stores it to the global or environment variable named secret

    var privateKey = postman.getEnvironmentVariable("privateKey");
    var body = request.method === 'GET' ? '' : request.data;
    var hash = CryptoJS.HmacSHA256(body, privateKey);
    var hashInHex = CryptoJS.enc.Hex.stringify(hash);
    postman.setEnvironmentVariable("secret", hashInHex);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment