How to create a raw body for POST request using PowerShell

Question:

I am trying a POST request using PowerShell. It takes body of type raw. I know how to pass form-data using PowerShell, however not sure for rawdata type. For a simple raw-data in Postman e.g.

I pass below in PowerShell and it works fine.

However, for a complex rawdata like below, I am unsure how to pass in PowerShell.

Answer:

My interpretation is that your second code block is the raw JSON you want, and you’re not sure how to construct that. The easiest way would be to use a here string:

Variable substitution works (because we used @" instead of @') but you don’t have to do messy escaping of literal " characters.

So what that means is that $source$ will be interpreted as a variable named $source to be embedded in the string followed by a literal $. If that’s not what you want (that is, if you want to literally use $source$ in the body), then use @' and '@ to enclose your here string so that powershell variables are not embedded.

Source:

How to create a raw body for POST request using PowerShell by licensed under CC BY-SA | With most appropriate answer!

Leave a Reply