Filter for rest sub-command does not work with payload from text file
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 the payload in a text file, this does not work.
Content of query.txt:
$filter=serviceName eq 'Virtual Machines' and armRegionName eq 'westindia'
Command:
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. But if southindia is appearing in the response, the filter is not working. How does one send a working filter payload from a file?
Also asked on stackoverflow: https://stackoverflow.com/questions/73206915/how-to-send-text-plain-body-via-azure-cli-rest-sub-command
@jiasli for awareness
@ParagDoke Apologies for the late reply. Thanks for reaching out to us and reporting this issue. Firstly, I see that you are sending a GET request which doesn't expect a request body.
The content $filter=serviceName eq 'Virtual Machines' and armRegionName eq 'westindia' should be a query string and should be a part of request url and not part of request body.
Methods like PUT, POST, PATCH expect the request body and that can be sent within the payload body:
> az rest -h
--body -b : Request body. Use @{file} to load from a file. For quoting
issues in different terminals, see
https://github.com/Azure/azure-
cli/blob/dev/doc/use_cli_effectively.md#quoting-issues.
Hi @ParagDoke. Thank you for opening this issue and giving us the opportunity to assist. We believe that this has been addressed. If you feel that further discussion is needed, please add a comment with the text “/unresolve” to remove the “issue-addressed” label and continue the conversation.
@navba-MSFT That API does not seem to support POST, PUT or PATCH methods. Effectively, I don't know of a way to filter results from the pricing API. Is there a working example that can be shared ?
/unresolved
Marking as unresolved. The title of this issue states "Filter for rest sub-command does not work with payload from text file". This was not addressed at all. The comment from @navba-MSFT merely speaks of using another method, which the API does not seem to support.
@ParagDoke Thanks for getting back. Could you please use the below command and check if that serves your requirement ? Awaiting your reply.
az rest --method get --url "https://prices.azure.com/api/retail/prices?$filter=serviceName eq 'Virtual Machines' and armRegionName eq 'southindia'"
For the record, as of 2022 Dec 8 7:30 UTC, POST, PUT and PATCH are not supported by the API endpoint.
Content of body.txt:
$filter=serviceName eq 'Virtual Machines' and armRegionName eq 'westindia'
Command outputs:
$ az rest --method post --uri "https://prices.azure.com/api/retail/prices" --body @body.txt
Method Not Allowed({"Message":"The requested resource does not support http method 'POST'."})
$ az rest --method put --uri "https://prices.azure.com/api/retail/prices" --body @body.txt
Method Not Allowed({"Message":"The requested resource does not support http method 'PUT'."})
$ az rest --method patch --uri "https://prices.azure.com/api/retail/prices" --body @body.txt
Method Not Allowed({"Message":"The requested resource does not support http method 'PATCH'."})
@ParagDoke As your rightly pointed it out currently it is just Get Azure Retail prices operation available now. See here: https://learn.microsoft.com/en-us/rest/api/cost-management/retail-prices/azure-retail-prices
So inorder to filter the response, use the $filter query string in the request url. Refer the below sample az cli command:
az rest --method get --url "https://prices.azure.com/api/retail/prices?$filter=serviceName eq 'Virtual Machines' and armRegionName eq 'southindia'"
Please let me know if you have any follow-up queries on this.
@ParagDoke I wanted to do quick follow-up to check if you had a chance to look at my above comment. Please let us know if you had any follow-up queries on this. Awaiting your reply.
@ParagDoke I hope you had a chance to look at my above comment. If you have any follow-up questions on this, please let me know. We would be happy to help.
@navba-MSFT If you see the title of this issue, what I am trying to report is that a payload from text file does not work. In my first post itself, I have already confirmed that adding $filter to the query string worked.
I will just conclude that it is not possible to provide query params from a text file.