Loading...

How to send an Azure REST API request

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:

  1. The request URL and HTTP method
  2. The authorization
  3. 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)

Jerry_0-1683619503555.png

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:

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.

Jerry_0-1683620056384.png

  • 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:

 

Authorization: Bearer eyJ0......DSA

 

 

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.

Jerry_2-1683620508723.png

 

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:

Jerry_3-1683620632785.png

 

Then setup the header used for authorization:

Jerry_4-1683620672714.png

 

Add headers and request body if they are required: (Added headers already showing in last image.)

Jerry_5-1683620778885.png

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.

Jerry_6-1683620865655.png

 

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:

 

$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:

JerryZhangMS_1-1683863756800.png

JerryZhangMS_2-1683863866980.png

Curl in Linux:

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:

JerryZhangMS_1-1683868558497.png

JerryZhangMS_0-1683868526076.png

 

Published on:

Learn more
Azure PaaS Blog articles
Azure PaaS Blog articles

Azure PaaS Blog articles

Share post:

Related posts

Azure Storage - TLS 1.0 and 1.1 retirement

Overview TLS 1.0 and 1.1 retirement on Azure Storage was previously announced for Nov 1st, 2024, and it was postponed recently to 1 year later...

1 year ago

Efficient Management of Append and Page Blobs Using Azure Storage Actions

  Overview In Azure Storage, Blob Lifecycle Management (BLM) allows you to automate the management of your data based on rules defined by...

1 year ago

[Azure AI Search] Internal Server Error when creating CMK encrypted objects

Scenario Customers follow the Microsoft doc to create CMK encrypted objects (data source, index etc.), but get the 500 Internal Serv...

1 year ago

Optimizing Azure Table Storage: Automated Data Cleanup using a PowerShell script with Azure Automate

Scenario This blog’s aim is to manage Table Storage data efficiently. Imagine you have a large Azure Table Storage that accumulates logs from ...

1 year ago

Optimizing Azure Table Storage: Automated Data Clean-up using a PowerShell script with Azure Automat

Scenario This blog’s aim is to manage Table Storage data efficiently. Imagine you have a large Azure Table Storage that accumulates logs from ...

1 year ago

Restoring Soft-Deleted Blobs with multithreading in Azure Storage Using C#

Blob soft delete is an essential feature that safeguards your data against accidental deletions or overwrites. By retaining deleted data for a...

1 year ago

Performing simple Azure Table Storage REST API operations using curl command.

The blog provides guidance to perform simple Table Storage REST API operations such as Create table, Delete Table, Insert entity, Delete entit...

1 year ago

Bulk delete all the old jobs from the batch account

Deleting a Job also deletes all Tasks that are part of that Job, and all Job statistics. This also overrides the retention period for Task dat...

1 year ago

Utilizing Azure Storage and Runbooks for scheduled automated backups of Azure SQL Databases

In this article, we are going to provide detailed steps to create a scheduled Azure SQL Database backup to storage account using automation. T...

2 years ago

[Azure Service Bus] JMS messages getting dead-lettered

The article discusses a problem where numerous messages end up in the dead letter queue (DLQ) when the JMS service bus consumer connects to th...

2 years ago
Stay up to date with latest Microsoft Dynamics 365 and Power Platform news!
* Yes, I agree to the privacy policy