How to send an Azure REST API request
Sometimes, user will need to send out REST API request to manage their resources in Azure platform, for example, when the REST API is upgraded but corresponding PowerShell module or User Interface are not upgraded yet. In this blog, it will contain three main parts:
- Common points of both ways to send request
- Send request manually with User Interface such as Postman
- Send request manually with command line, such as PowerShell in Windows and Curl in Linux
Common points of both ways to send request:
No matter user wants to send request by which way, the next three points are always the most important:
- The request URL and HTTP method
- The authorization
- The header and the request body (payload)
The request URL and HTTP method
From the official document, there will always be the information as following one. (Resources - List - REST API (Azure Resource Management) | Microsoft Learn as example here)
It's easy to find out the HTTP method here is GET and the complete URL to be called. The only thing to pay attention is to verify if there is any part with "{}". If there is such kind of part, it means that it needs to be replaced by your own resource/environment data. For example, for the above REST API, when we call it, it will be like:
https://management.azure.com/subscriptions/5102xxxx-xxxx-xxxx-xxxx-xxxxa4473453/resources?api-version=2021-04-01
P.S. Please pay attention to the format. If the final request URL is like https://management.azure.com/subscriptions//5102xxxx-xxxx-xxxx-..., then this request must be failed.
The authorization
For different REST API servers, we need to use different ways to authenticate. For example, here are some special situations:
- For Storage REST API, Authorize requests to Azure Storage (REST API) | Microsoft Learn
- For Azure Batch REST API, Authenticate Requests to the Azure Batch Service | Microsoft Learn
But most of the REST API requests in Azure will be sent to ARM (Azure Resource Manager). The most important point to identify this is the first part of the URL. If the first part of the URL is https://management.azure.com, then this must be an ARM REST API. For such kind of requests, we can always use the following way to authenticate.
- From Azure Portal, open the Cloud Shell window. (For the users not allowed to use Cloud Shell, you can also install the CLI in local machine. How to install the Azure CLI | Microsoft Learn)
- Make sure the mode is PowerShell, then after it automatically login, run command az account get-access-token. (For the users installing the Azure CLI in local machine, please run az login before running the above command)
- Please copy out the value of the accessToken from the response and remove all the line breaker to make them as a simple but long word. Then, the header needed for the authorization will be with name Authorization and be with value Bearer {tokenvalue}. (The space between keyword Bearer and first letter of token value is necessary) For the above example, the header will be like:
The header and the request body (payload)
This part is not always required. But for some REST API to create/update a resource, such as Resource Groups - Create Or Update - REST API (Azure Resource Management) | Microsoft Learn, there will be a request body or/and request header part with explanation. We can find out which headers/request body parts are necessary and what they stand for.
Send request manually with User Interface such as Postman
To be able to follow this part, please kindly do confirm that the Postman is already installed in your computer. Postman can be downloaded from Download Postman | Get Started for Free
We need to setup the HTTP method and URL at first:
Then setup the header used for authorization:
Add headers and request body if they are required: (Added headers already showing in last image.)
P.S. The request body of most Azure official REST API is blank or JSON format.
Once all the above steps are done, we can simply click on send request button and see the response code, response time and response body if there is.
Send request manually with command line, such as PowerShell in Windows and Curl in Linux
The way of sending request by command is similar to the above part. User only needs to make sure the important information mentioned in first part and put it into the command. The following part is the example script in both Windows and Linux system: (Using Resource Groups - Create Or Update - REST API (Azure Resource Management) | Microsoft Learn as example here)
PowerShell in Windows:
Result:
Curl in Linux:
Result:
Published on:
Learn moreRelated posts
Microsoft Fabric API for GraphQL™ for Azure Cosmos DB Mirroring
The Microsoft Fabric API for GraphQL™ provides a data access layer that enables you to query multiple data sources quickly and efficiently usi...
Power Automate Custom Connector – Implement Retry Function, Azure Application Insights
Another journey on how to create a Power Automate Custom Connector with .NET API 8. Last week, we created a .NET API Project that implements t...
Azure SDK Release (November 2024)
The Azure SDKs release every month. This post includes the month's highlights and release notes. The post Azure SDK Release (November 2024) ap...
Azure Confidential Clean Rooms Demonstration
Azure Database for PostgreSQL Flexible Server - Elastic Clusters, faster disks, and AI updates
Increase scalability, optimize performance, and integrate advanced AI features with Azure Database for PostgreSQL Flexible Server. Scale up wi...
Disconnected operations for Azure Local
Introducing the new Linux-based Azure Cosmos DB Emulator (Preview)
We are excited to announce the preview release of the new Linux-based Azure Cosmos DB Emulator! This latest version is built to provide faster...
Azure Cosmos DB Shines at Microsoft Ignite 2024!
Microsoft Ignite 2024 took over the Windy City this week, bringing with it new technological innovation and exciting product announcements apl...