question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Azure REST API for SQL not working for CLI but works for Post Man

See original GitHub issue

Describe the bug

Command Name az rest

Errors:

Bad Request({"error":{"details":[{"code":"InvalidResourceIdSegment","message":"","target":"parameters.properties.weeklyRetention"}],"code":"InvalidResourceIdSegment","message":"The 'parameters.properties.weeklyRetention' segment in the url is invalid."}})

To Reproduce:

Steps to reproduce the behavior. Note that argument values have been redacted, as they may contain sensitive information.

# Language : PowerShell
$server = "YOUR_AZURE_SQL_SERVER"
$database = "YOUR_AZURE_SQL_DATABASE"
$weeklyRetention = "P2W"
$policyName = "default"
$apiVersion = "2019-06-01-preview"

$srvs = az sql server list --query "[?name=='$server']" | ConvertFrom-Json
$srv = $srvs[0]
$dbs = az sql db list --resource-group $srv.resourceGroup --server $srv.name --query "[?name=='$database']" | ConvertFrom-Json
$db = $dbs[0]

# This works 👍 
az rest --method "GET" --uri "$($srv.id)/databases/$($db.name)/backupLongTermRetentionPolicies/$($policyName)?api-version=$apiVersion" | ConvertFrom-Json

# This Fails 👎 
az rest --method "PUT" --uri "$($srv.id)/databases/$($db.name)/backupLongTermRetentionPolicies/$($policyName)?api-version=$apiVersion" --header "Content-Type=application/json" --body "{ ""properties"": { ""weeklyRetention"": ""$weeklyRetention"" } }" --debug

Expected Behavior

Should update with 202 response:

{
    "operation": "UpsertDatabaseBackupArchivalPolicyV2",
    "startTime": "2020-05-05T15:04:16.397Z"
}

Environment Summary

Windows-10-10.0.14393-SP0
Python 3.6.6

azure-cli 2.2.0

Additional Context

Also note that setting backupShortTermRetentionPolicies on the same resource works without a problem:

az rest --method "PUT" --uri "$($srv.id)/databases/$($db.name)/backupShortTermRetentionPolicies/$($policyName)?api-version=$apiVersion" --header "Content-Type=application/json" --body "{ ""properties"": { ""retentionDays"": 35 } }"

So I know it’s not an issue with the headers or how I pass the JSON body.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
jiaslicommented, May 9, 2020

Typical PowerShell issue https://github.com/PowerShell/PowerShell/issues/1995. 😕

This command should fail as well because the quote quotes are lost:

az "{ ""properties"": { ""retentionDays"": 35 } }" --debug
Command arguments: ['{ properties: { retentionDays: 35 } }', '--debug']

Please see my latest PR https://github.com/Azure/azure-cli/pull/13419 where I am trying to make this clear and providing some workarounds.

1reaction
yonzhancommented, May 6, 2020

add to S170

Read more comments on GitHub >

github_iconTop Results From Across the Web

Azure REST APIs with Postman (2021) - YouTube
Here's the 2021 edition of my very popular Azure REST APIs with Postman video and blog posts.Lots of things have changed since my...
Read more >
Azure REST APIs with Postman (Dec 2017) - Jon Gallant
The Azure REST APIs require a Bearer Token Authorization header. The docs do a great job explaining every authentication requirement, but do ...
Read more >
Calling Azure REST API via curl - Davide Mauri - Medium
Calling Azure REST API via curl. A straightforward post to invoke Azure REST API via simple HTTP calls. In these days I needed...
Read more >
Issues using rest api body in powershell with Azure cli
You can use powershell CMD Invoke-AzRestMethod with put method it works similarly as az rest method. Share.
Read more >
GET REST API works with postman but gives error from VB ...
but everytime same exception. my codes are working if i put same API on another web server with https but it does not...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found