Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

az rest fails to parse json whitespace #10562

Closed
jurgyy opened this issue Sep 18, 2019 · 7 comments
Closed

az rest fails to parse json whitespace #10562

jurgyy opened this issue Sep 18, 2019 · 7 comments
Assignees
Labels
OKR3.4 Candidate For OKR3.4 rest az rest

Comments

@jurgyy
Copy link
Contributor

jurgyy commented Sep 18, 2019

Describe the bug
Using powershell, az rest fails to parse my json correctly. I already read issue #9742 where @konrad-jamrozik had troubles parsing json correctly and he ended up having to escape the quotes (with \" not `", the .ps1 escape character). So I tried that, but it didn't solve all my problems. After some fiddling I found the problem: It was the whitespace in a string.

To Reproduce

The following code fails:

$uri = "https://management.azure.com/subscriptions/{subscriptionId}/resourceGroups/myRsg/providers/Microsoft.ApiManagement/service/myApim/apis/myApi?api-version=2019-01-01"

$oauthServiceName = "myOauthService"
$description = "my api description"
$displayName = "my api"
$serviceUrl = "http://localhost"
$uniqueApiPath = "myapi/"

$payloadBody = @{
    properties = @{
        authenticationSettings = @{
            authorizationServerId = $oauthServiceName
        }
        description = $description
        displayName = $displayName
        serviceUrl = $serviceUrl
        path = $uniqueApiPath
        protocols = @(
            "http"
        )
    }
}| ConvertTo-Json -Compress

$payloadBody = $payloadBody -replace "`"", "\`""

az rest --method put --headers "Content-Type=application/json" --uri $uri --body $payloadBody

Error:

az: error: unrecognized arguments: api description","serviceUrl":"http://localhost","authenticationSettings":{"authorizationServerId":"myOauthService"},"path":"myapi/","protocols":["http"],"displayName":"my api"}}

While if we remove the whitespace from both the description and display name it will succeed:

$description = "myApiDescription"
$displayName = "myApi"

Expected behavior
When using the portal whitespace is allowed, so I expect the json to parse correctly when using whitespace.

Environment summary
Install Method: MSI
CLI version: 2.0.73
OS: Windows 10
Shell Type: Powershell

@Juliehzl
Copy link
Contributor

@jurgyy Thanks a lot for your feedback. I am out of office now and I will have a look when come back.

@jurgyy
Copy link
Contributor Author

jurgyy commented Sep 30, 2019

Any updates?

In the meanwhile, some debug information:

With the following body

{\"foo\":\"this is foo\",\"bar\":\"this is bar\",\"description\":\"this is desc\",\"baz\":\"this is baz\"}

az rest with the --debug flag shows the problem

 Command arguments: [
    'rest',
    '-m',
    'put',
    '--headers',
    'Content-Type=application/json',
    '-u',
    'https://management.azure.com/subscriptions/{subscriptionId}?api-version=2019-01-01',
    '-b',
    '{"foo":"this', 'is', 'foo","bar":"this', 'is', 'bar","description":"this', 'is', 'desc","baz":"this', 'is', 'baz"}',
    '--debug'
]
...
az_command_data_logger : command args: rest -m {} --headers {} -u {} -b {} {} {} {} {} {} {} {} {} --debug
...
cli.azure.cli.core.parser : az: error: unrecognized arguments: is foo","bar":"this is bar","description":"this is desc","baz":"this is baz"}
az: error: unrecognized arguments: is foo","bar":"this is bar","description":"this is desc","baz":"this is baz"}

Notice the strange comma separation of the json in the command arguments and the number of {} in az_command_data_logger.

Edit:

I've traced the problem back to Knack and opened an issue there: microsoft/knack#165
I think this issue can be closed.

@haroldrandom haroldrandom added the rest az rest label Oct 25, 2019
@jurgyy
Copy link
Contributor Author

jurgyy commented Nov 27, 2019

If anyone encounters this issue before it is fixed I've found that writing the json to a temporary file and reference that works fine:

$dict | ConvertTo-Json -Depth 100 > "./tmp/body.json"  
az rest -m put --headers "Content-Type=application/json" -u $uri -b "@tmp/body.json"

@yonzhan yonzhan added this to the S163 milestone Dec 1, 2019
@yonzhan yonzhan modified the milestones: S163, S164 Dec 31, 2019
@yonzhan yonzhan modified the milestones: S164, S165 Feb 1, 2020
@yonzhan yonzhan assigned zhoxing-ms and unassigned Juliehzl Feb 1, 2020
@yonzhan yonzhan modified the milestones: S165, S166 Feb 15, 2020
@yonzhan yonzhan modified the milestones: S166, S167 Mar 5, 2020
@yonzhan yonzhan modified the milestones: S167, S168 Mar 28, 2020
@zhoxing-ms
Copy link
Contributor

@jurgyy Hi, this problem is also related to the parsing of :\, which has a special meaning in PowerShell, like: about_providers, about_locations
So : and \ can be separated by spaces in the script as work around, like:

$payloadBody = $payloadBody -replace "`"", "\`"" -replace ":\\", ": \"

You can have a try. If you still have any questions, please feel free to let us know~

@yonzhan yonzhan modified the milestones: S168, S169 - For Build Apr 18, 2020
@yonzhan
Copy link
Collaborator

yonzhan commented Apr 18, 2020

@jurgyy any feedback ?

@yonzhan yonzhan added the OKR3.4 Candidate For OKR3.4 label Apr 18, 2020
@jurgyy
Copy link
Contributor Author

jurgyy commented Apr 20, 2020

I haven't tested it myself yet, but if it works this workaround is fine I guess. Not much you can do about Powershell's special characters anyway. Thanks!

The issue can be closed, I think.

@zhoxing-ms
Copy link
Contributor

@jurgyy OK, I'll close it first. If you still have any other questions, please feel free to let us know~

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
OKR3.4 Candidate For OKR3.4 rest az rest
Projects
None yet
Development

No branches or pull requests

7 participants