Send Message to Azure Service Bus via Postman using SAS Token in 1 Step

Chia Li Yun
Javarevisited
Published in
3 min readDec 3, 2021

Azure Service Bus is a fully managed enterprise message broker that offers

  • Message Queues
  • Publish-Subscribe Topics

It is usually used to decouple applications and services which brings about the following advantages:

  • Load-balancing work across competing workers
  • Safely routing and transferring data and control access service and application boundaries
  • Coordinating transactional work that requires a high-degree of reliability
Photo by Martin Shreder on Unsplash

While we are developing our solution, we might want to test it by sending messages to our queue/topic.

There are 2 ways we can go about sending messages:

  1. Explorer on Microsoft Azure Portal itself
  2. Service Bus service Restful API

In this article, we will be focusing on the 2nd approach via Postman. Explorer can be a convenient option if you only want to send a simple test once in awhile.

However, it will clear off your “message body” after it has been sent out successfully and this may be quite disruptive to the testing process if you will be using a similar input message.

Let’s look at the restful API provided by Microsoft to access our Service Bus namespace. It is a POST request to http{s}://{serviceNamespace}.servicebus.windows.net/{queuePath or topicPath}/messages with mandatory properties likeAuthorization , Content-Type and BrokerProperties in the request header.

The value of Authorization can be one of the following type:

  1. Azure Active Directory (Azure AD) JSON Web Token (JWT)
  2. Shared Access Signature (SAS) Token

We will be focusing getting authorised using SAS token. You may read up more about the token structure here.

Given that we need a token, what we normally have to do is to run another separate POST request just to generate a new token. However, this can be very tedious and likewise disruptive to our testing process.

Pre-Request Script

I would like to introduce to you — Pre-request Script feature in Postman. This allows us to run some code and assign values to variables which we can use in our request.

In the POST request to create a message, we need 2 variables — access token and broker properties (optional, use only if you need to set additional metadata informations like SessionId , PartitionKey etc.). You will need to update the following code with your SharedKeyName ,SharedKey and URI . createSharedAccessToken is responsible to form the SAS Token based on the structure.

Postman Pre-request Script

Your Postman request should look something like this:

Example: Pre-request Script in Postman
Example: Headers

Summary

Voilà! Now, we can send message to our queue/topic in 1 simple step!

Hope you will find this approach simple and intuitive! Would really appreciate if you could help to share this article with your peers so that this can reach out to a wider audience! 🙏

Thank you for reading! 🤓

Side note: If you are not a Medium member yet, click here to join!

References

--

--

Chia Li Yun
Javarevisited

Recent graduate from university. Always excited about the new technologies and love to share with the tech community here!