How do I place properly formatted JSON in an http POST body?

102 views Asked by At

I am generating Outlook Actionable Messages via Powershell. I need to put JSON in the body of an HTTP post request like this example for Microsoft's site (please note the example is just above this link, I can't copy and paste for some reason, so please scroll up a little after you click it)

https://learn.microsoft.com/en-us/outlook/actionable-messages/adaptive-card#reporting-actionhttp-execution-success-or-failure

I've noticed that the. "body": "{{nameInput.value}}" portion needs to be JSON but escape characters are required or it does not work. Is there a good way in Powershell to generate this output? I have created Powershell objects and used convertto-json, which creates JSON but not with the appropriate escape characters.

Here's an example of an object created in PowerShell

$appointment = [pscustomobject]@{
        Subject = "Foo"
}

Converting that to JSON gives me this:

$appointment | ConvertTo-Json
{
    "Subject":  "Foo"
}

However, when I put this in the http POST body, I need something like this:

"body": 
     "{
           \"subject\":\"foo\"
      }",

Note the escaping around the quotation marks.

Thanks!

0

There are 0 answers