Why is my Invoke-RestMethod JSON body being rejected?

November 13, 2026 3 answers
Asked by Rovsy

I'm converting a curl POST to Invoke-RestMethod. The curl body is an array of objects, but my PowerShell version uses a here-string with one object piped to ConvertTo-Json. The API returns an invalid JSON error. What am I missing?

Answers

radish+on~standby

There are two differences in your request. The here-string already contains JSON, so piping it to ConvertTo-Json turns it into a JSON string rather than an object. The added backslashes are part of that string encoding.

Also, the curl example sends an array. Even for one item, keep the square brackets around the object and pass the here-string directly as the body. Set ContentType to application/json and keep the bearer authorization header.

Rovsy:

Passing the JSON directly with the square brackets worked. I had noticed the backslashes but didn't realize the whole body had become a string.

Reply to this answer
aisle72_sidequest

You can also build the payload as PowerShell data instead of writing JSON by hand. Create an array of hashtables, then use ConvertTo-Json -InputObject $array to serialize it. Using InputObject matters for a single item because it preserves the top-level array instead of piping its elements individually.

Check the resulting $body before sending it. It should start with a square bracket, not a quote or an opening brace. If the data gets more deeply nested, set an appropriate ConvertTo-Json -Depth as well.

remote.control.truce:

That route is handy when the values come from variables or need JSON escaping. For a fixed payload, the raw JSON here-string is fine too.

Reply to this answer
porcelain-parking-pass

Compare the actual payloads, not just the property names. Your earlier example had an object containing a list of strings, while this curl request has a top-level list of objects. Those are different JSON shapes, and an endpoint can expect one rather than the other.

Reply to this answer

Add your answer

Answers are reviewed before publication.

Related Discussions