Showing posts with label NotificationHub. Show all posts
Showing posts with label NotificationHub. Show all posts

March 25, 2026

Using Azure Notification Hub REST from Bruno

Task

Having migrated away from rather bloated Postman to using Bruno, I needed to make REST API calls to Azure Notification Hub. It requires specific Authentication heared and you need to build it in a Pre Request Script.

Solution

Here's the script. Use Header: Authorization with value {{azure-authorization}}. Enjoy!

const CryptoJS = require('crypto-js');

function getAuthHeader(targetUri, ruleId, sharedKey,

expiresInMins) {

targetUri = encodeURIComponent(targetUri.toLowerCase()).toLowerCase();

// Set expiration in seconds

var expireOnDate = new Date();

expireOnDate.setMinutes(expireOnDate.getMinutes() + expiresInMins);

var expires = Date.UTC(expireOnDate.getUTCFullYear(), expireOnDate

.getUTCMonth(), expireOnDate.getUTCDate(), expireOnDate

.getUTCHours(), expireOnDate.getUTCMinutes(), expireOnDate

.getUTCSeconds()) / 1000;

var tosign = targetUri + '\n' + expires;

// using CryptoJS

var signature = CryptoJS.HmacSHA256(tosign, sharedKey);

var base64signature = signature.toString(CryptoJS.enc.Base64);

var base64UriEncoded = encodeURIComponent(base64signature);

// construct autorization string

var token = "SharedAccessSignature sr=" + targetUri + "&sig="

+ base64UriEncoded + "&se=" + expires + "&skn=" + ruleId;

//console.log("signature:" + token);

return token;

}

const baseUrl = bru.getFolderVar('url');

const currentTemplateUrl = req.getUrl();

// Replace the template with the real value

const resolvedUrl = currentTemplateUrl.replace("{{url}}", baseUrl);


bru.setVar('azure-authorization', getAuthHeader(resolvedUrl, "<<SharedAccessKeyName>>", "<<SharedAccessKey>>", 1));

bru.setVar('current-date',new Date().toUTCString());