2

I am using powershell (specifically Azure CLI) to get data from a service, add a value to a list and send a patch/post to an URL.

This is the URL to get the list of data:

$uriList = az rest --method get --uri "https://graph.microsoft.com/beta/applications/$objectId" --header "$header" --query spa.redirectUris | ConvertFrom-Json

This returns an array of strings.

['a', 'b', 'c', 'd', 'e']

if ($uriList -notcontains "f") {
    $uriList  += "f"
}

$uriListJSON = $uriList | ConvertTo-Json

Above, I have added "f" to the array and I want to send it to the url as follows:

$body = "{spa:{redirectUris: $uriListJSON}}"

az rest --method patch --uri "https://graph.microsoft.com/beta/applications/$objectId" --headers "$header" --body "$body"

I get an error:

Unable to read JSON request payload. Please ensure Content-Type header is set and payload is of valid JSON format."

Please advice on what I am doing wrong. I outputted uriListJSON to a file and ran it against a json validator and it passed.

When I hardcode everything to a url like

az rest --method patch --uri "https://graph.microsoft.com/beta/applications/$objectId" --headers $header --body "{spa: {redirectUris: ['a', 'b', 'c', 'd', 'e', 'f']}}"

it works like a charm.

1 Answer 1

2

Your from-and-to JSON conversion results in a string with - standards-compliant - embedded double-quoting ("):

PS> ("['a', 'b', 'c', 'd', 'e']" | ConvertFrom-Json) + 'f' | ConvertTo-Json -Compress

["a","b","c","d","e","f"]

Since you're passing the resulting string to an external program - az - the sad reality as of PowerShell 7.2 is that an extra, manual layer of \-escaping of embedded " characters is required in arguments.
This may get fixed in a future version, which may require opt-in. See this answer for details.

Therefore:

$uriListJSON = ("['a', 'b', 'c', 'd', 'e']" | ConvertFrom-Json) + 'f' | ConvertTo-Json -Compress

$body = "{ \`"spa\`": { \`"redirectUris\`": $($uriListJSON -replace '"', '\"') } }"

Note that while it may not be necessary for az, the above uses a strictly standards-compliant JSON string in which the property names of the verbatim part of your JSON string too are "..."-quoted, which requires both escaping for PowerShell (`") and manual \-escaping for the external program; in combination: \`"

Sign up to request clarification or add additional context in comments.

8 Comments

Thanks for your advice. I tried it. Its not working. This is a screenshot of what I have done so far. imgur.com/a/WeTZCQP
@a2441918, please see my update - I hadn't noticed that the verbatim part of your JSON needs"..." quoting and therefore \ -escaping too.
Tried that. Running into a string issue. imgur.com/a/qWiJUVc
@a2441918: Oops! My bad: forgot to escape the embedded " for PowerShell as well - please see my update.
Sorry. Still the issue. imgur.com/a/UHqgzJq Do you want me to run any additonal commands to see if the data is in the correct format?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.