SarahB
SarahB

Reputation: 328

Unable to post message to Azure Service Bus Queue from Azure API using Managed Identity

I am trying to test out the sample code by azure "Authenticate using Managed Identity to access Service Bus" and its on github:

Azure API Management Policy Snippets

What I have done is in below steps:

  1. Created an Azure API Management Service. In this I added an API which has a POST method
  2. I also enabled a System Generated Managed Identity for this APIM
  3. I created a Service Bus and create a queue
  4. I added the managed identity to a role of "Azure Service Bus Data Sender" on the queue.
  5. Last, I modified the code from azure to have names from objects I created above and it looks like below:

enter image description here

On running a test on API I get error: "500 Internal Server Error". The message of course is not being sent. Any idea what I may be doing wrong here? Help appreciated.

Upvotes: 0

Views: 1151

Answers (2)

SarahB
SarahB

Reputation: 328

I found the issue which is almost what @pankaj has suggested. I made a mistake when I created a POST operation on the api I created. When we add an operation for an api in azure apim, it asks for a URL also as shown below: [![Azure API - adding an Operation][1]

The issue was that I added a random text something like "/message". What this does is, it adds this path to the set-backend-service base-url

As that modified backend url was incorrect, so was the reason for error. I updated it to just a "/" in the POST operation path and the error got resolved.

I then again faced this issue when my request had some query parameters. It was again appending these query parameters to the backend url. To resolve that I added below:

<set-query-parameter name="{{replace-with-query-param}}" exists-action="delete" />

Thank you everyone for trying to help me. Hopefully, this post will be of some help to others [1]: https://i.sstatic.net/Rechc.jpg

Upvotes: 1

Pankaj More
Pankaj More

Reputation: 533

It seems to be issue with your Authorization header and the calling URL. All other steps looks good.

Please find below Policy code snippet which works fine at my end. I am able to send the data to service bus successfully. My operation endpoint is '/messages' with POST method. enter image description here

Upvotes: 1

Related Questions