Parag Doke
Parag Doke

Reputation: 863

How to send text/plain body via Azure CLI rest sub-command

Trying to figure how to capture rest payload in a text file for Azure CLI "rest" sub-command.

This works:

az rest --method get --uri "https://prices.azure.com/api/retail/prices?$filter=serviceName eq 'Virtual Machines' and armRegionName eq 'westindia'"

But if I save this in a text file, the filter does not work:

C:\Users\foo> type query.txt
$filter=serviceName eq 'Virtual Machines' and armRegionName eq 'westindia'
C:\Users\foo> az rest --verbose --method get --uri "https://prices.azure.com/api/retail/prices" --body @query.txt --headers Content-Type=text/plain
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
Request URL: 'https://prices.azure.com/api/retail/prices'
Request method: 'GET'
Request headers:
    'User-Agent': 'python/3.10.5 (Windows-10-10.0.22000-SP0) AZURECLI/2.38.0 (MSI)'
    'Accept-Encoding': 'gzip, deflate'
    'Accept': '*/*'
    'Connection': 'keep-alive'
    'Content-Type': 'text/plain'
    'x-ms-client-request-id': '<immaterial>'
    'CommandName': 'rest'
    'ParameterSetName': '--verbose --method --uri --body --headers'
    'Content-Length': '74'
Request body:
$filter=serviceName eq 'Virtual Machines' and armRegionName eq 'westindia'
Response status: 200
Response headers:
    'Content-Length': '10821'
    'Content-Type': 'application/json; charset=utf-8'
    'Date': 'Tue, 02 Aug 2022 11:24:55 GMT'
    'Server': 'Microsoft-IIS/10.0'
    'Cache-Control': 'no-cache'
    'Content-Encoding': 'gzip'
    'Expires': '-1'
    'Pragma': 'no-cache'
    'Set-Cookie': 'ARRAffinity=<immaterial>;Path=/;HttpOnly;Secure;Domain=prices.azure.com, ARRAffinitySameSite=<immaterial>;Path=/;HttpOnly;SameSite=None;Secure;Domain=prices.azure.com'
    'Vary': 'Accept-Encoding'
    'X-AspNet-Version': '4.0.30319'
    'X-Powered-By': 'ASP.NET'
Response content:
{"BillingCurrency":"USD","CustomerEntityId":"Default","CustomerEntityType":"Retail","Items":[{"currencyCode":"USD","tierMinimumUnits":0.0,"retailPrice":0.1955,"unitPrice":0.1955,"armRegionName":"southindia", ...

Response truncated for brevity. But notice that southindia appears in the response. So clearly, that filter is not working.

I understand that my payload (content of query.txt) is not URL encoded.

How does one send a working filter payload from a file?

Unrelated, but sharing for full context: I am trying to build a filter matching sku names. See Find cheapest spot supported size / sku via az CLI or terraform provider

Upvotes: 0

Views: 438

Answers (1)

Parag Doke
Parag Doke

Reputation: 863

Isn't possible to send payload via an external text file. See https://github.com/Azure/azure-cli/issues/23465#issuecomment-1364631686.

Upvotes: 0

Related Questions