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:
No matter user wants to send request by which way, the next three points are always the most important:
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.
For different REST API servers, we need to use different ways to authenticate. For example, here are some special situations:
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.
Authorization: Bearer eyJ0......DSA
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.
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.
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)
$URI = "https://management.azure.com/subscriptions/5102xxxx-xxxx-xxxx-xxxx-xxxxxx473453/resourcegroups/rgbypowershell?api-version=2021-04-01"
$headers = [ordered]@{"Content-Type"="application/json"; "Authorization"="Bearer eyJ0eXAiOiJ..............ty9wdI5GmDqA"}
$method = "PUT"
$body = '{"location": "eastus"}'
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
$response = Invoke-WebRequest -URI $URI -Method $method -Headers $headers -Body $body
Result:
curl -X PUT -H "Content-Type: application/json" -H "Authorization: Bearer eyJ0eX.....AA" -d '{"location": "westus3"}' https://management.azure.com/subscriptions/5102xxxx-xxxx-xxxx-xxxx-xxxxxx473453/resourcegroups/rgbycurl?api-version=2021-04-01
Result:
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.