Search code examples
azureazure-devopsazureservicebus

Send a message to a service bus queue with az rest


I want to send a message to a service bus with az rest, but got the error Can't derive appropriate Azure AD resource from --url to acquire an access token. If access token is required, use --resource to specify the resource

My code looks like this:

requestUri="https://${serviceBusNamespace}.servicebus.windows.net/${queueName}/messages"
az rest --method post --uri "$requestUri" --body "$messagePayload"

Unfortunately I could not find how to specify the --resource parameter correctly to make it work.


Solution

  • The --resource parameter must be set to "https://servicebus.azure.net" for the az rest to work. The full code for a shell script would therefore look like:

    #!/bin/bash
    
    # login to azure - may have to specify subscription
    az login
    
    # Azure Service Bus credentials
    serviceBusNamespace="namespaceName"
    resourceGroup="my-rg"
    subscriptionId="000-111-222-333"
    queueName="test"
    
    # Get the message from the command line argument
    messagePayload="Hello world!"
    
    # Construct the request URI
    requestUri="https://${serviceBusNamespace}.servicebus.windows.net/${queueName}/messages"
    # Send the message using az rest command
    az rest --method post --uri "$requestUri" --body "$messagePayload" --resource "https://servicebus.azure.net"