AzureTipsAndTricks/blog/tip223.md

4.5 KiB
Исходник Постоянная ссылка Ответственный История

type title excerpt tags share date
post Tip 223 - Azure REST APIs with Postman How to use Azure REST APIs with Postman
Web
true 2019-09-05 02:00:00

::: tip

💡 Learn more : Azure REST API Browser

:::

This post was brought to you by Kumar Allamraju.

Azure REST APIs with Postman

Postman is a powerful tool for performing integration testing with your APIs. It allows us to perform repeatable, reliable tests that can be automated and used in a variety of environments and includes useful tools for persisting data and simulating how a user might actually be interacting with the system. In this article we will demonstrate how to use Postman to test/validate the REST APIs.

  1. Download and install the Postman native app for your operating system

  2. Launch the postman app and create a new environment for this project. I named it as "azuretips". To learn more about creating an environment refer to this link

  3. Create a service principal from your Azure Cloud Shell. This way you don't have to install Azure CLI in your local desktop.

az ad sp create-for-rbac -n "your service principal name"

e.g. az ad sp create-for-rbac -n "azuretips"
Creating a role assignment under the scope of "/subscriptions/11389280-b2a2-***********"
  Retrying role assignment creation: 1/36
  Retrying role assignment creation: 2/36
  Retrying role assignment creation: 3/36
{
  "appId": "d2846107-f7ae-439b-8a16-da440d52e950",
  "displayName": "azuretips",
  "name": "http://azuretips",
  "password": "00593485-8bda-4c2b-8c4e-2ab52241c24c",
  "tenant": "72f98xxx-86f1-41af-****"
}

  1. To get your azure subscription id execute the following command in your cloud shell
az account show --query id
"11389280-b2a2-4183-b757-xxxxxx"

  1. Input the tenantid (tenant), clientid (appId), clientsecret (password), subid (subscription ID) in postman's "azuretips" environment section that was created in Step # 2. See the screenshot below
  1. Create a new collection in your postman app to store all your REST calls in an isolated section. To learn more about collections in postman, refer to this link

  2. We need to get the bearer token before executing any Azure REST API calls.

  3. Issue a POST request to

https://login.microsoftonline.com/:tenantid/oauth2/token

with the following details

	- Params Tab >> Path Variables.

	Key: tenantid
	Value: {{tenantid}}

	- Authroization Tab

	Type: Inherit auth from parent

	- Headers Tab

	Content-Type: application/x-www-form-urlencoded

	-  Body tab

	grant_type: client_credentials
	client_id: {{clientid}}
	client_secret: {{clientsecret}}
	resource: https://management.azure.com/

	- following value in the Tests tab
	pm.environment.set("bearerToken", pm.response.json().access_token);

You might be thinking this is a lot of work. But this is just a one time setup in Postman and you can share,re-use or duplicate this environment with your co-workers.

After getting the bearer token you can execute the Azure REST APIs for getting Resource Groups, details about a particular Resource Group, VNets etc..

As an example I issued a GET request to get details about a resource group in my azure subscription. In the Header section add a key "Authorization" with value Bearer {{bearerToken}}

See the screenshot below

Pls review this page to use the right GET or POST requests for Azure REST APIs

Conclusion

Postman is a comprehensive API testing tool that makes it easy to set up automated tests. You can aggregate the tests and requests youve created into a single automated test sequence. It is the only complete API development and testing environment and provides a single source of truth to your organization. In this article we learned how to use Postman with Azure REST APIs.