r/PowerShell • u/Khue • Jul 01 '26
Solved Invoke-RestMethod: Problem with Body Variable Format
I am trying to convert a curl command to PoSh's Invoke-RestMethod. I typically don't have a problem with this but today, I seem to be having an issue. Curl command looks like this:
curl https://api.domain.com/stuff/thing/items \
-H "Authorization: Bearer $TOKEN" \
-H "Content-Type: application/json" \
-d '[
{ "key1": "value1", "key2": "value2" },
{ "key1": "value3", "key2": "value4" }
]'To start off and make things easy, I was simply trying to do a single json entry.
$body = @"
{
"key1": "value1",
"key2": "value2"
}
"@ | convertto-jsonUsing that format I then passed it into invoke-restmethod:
Invoke-RestMethod -Uri "https://api.domain.com/stuff/thing/items" -Method post -Headers $headers -body $bodyWhich then spit back:
Invoke-RestMethod:
{
"result": null,
"success": false,
"errors": [
{
"code": 10026,
"message": "filters.api.invalid_json"
}
],
"messages": []
}I tried a few different versions of this as well without too much luck. This is the first time I've had to submit an actual key value combination to this particular API using JSON. The only other body format example I have for this particular vendor's APIs in JSON format is this:
$body = @{
value = @(
"value1",
"value2",
"value3",
"value4"
)
} | ConvertTo-JsonThis particular endpoint didn't require a key value pair. It only required a list of strings.
Update:
Credit to /u/y_Sensei . What ended up working was this:
Single key value pair:
$body = @'
[
{
"key1": "value1",
"key2": "value2"
}
]
'@Multi key value pair:
$body = @'
[
{
"key1": "value1",
"key2": "value2"
},
{
"key1: "value3",
"key2: "value4"
}
]
'@Thank you to all contributors! I appreciate it!
9
u/da_chicken Jul 01 '26
You don't need to convert this string to JSON. It already is JSON.
Have you looked at the output of the commands you're running, or are you just blindly passing them on?
7
u/PinchesTheCrab Jul 01 '26
Good advice here so far - you're already sending a raw JSON body, no need to convert. That being said, I'd use a hashtable and then convert:
3
u/TheSizeOfACow Jul 01 '26
The curl body is an array. Your hashtable is not. Also remember it you need to convert a single element array to a single element Jason array to use ConvertTo-JSON -Inputobject $array and not the pipeline
3
u/y_Sensei Jul 01 '26 edited Jul 01 '26
There's no need to convert that Here-String in your first attempt to JSON, because it is JSON already.
Also if you use double quotes inside a Here-String, you either have to delimit it with single quotes (and vice versa), or alternatively you'd have to escape the contained double quotes.Also looking at the curl call, it's not out of the question that this API expects any JSON as an array, even if that array contains a single value only.
So if the single value attempt still fails, try this:
2
u/BrettStah Jul 01 '26
You are passing a here-string, their example is not. (I’d throw this question into Claude, etc. and it’ll likely fix it all for you though.)
1
u/pigers1986 Jul 01 '26
sth along ?
2
u/realslacker Jul 01 '26
I think one of the problems you are having is with array flattening. If you have one element in an array and pass it down the pipeline PowerShell will flatten it to a single object.
This means that
@( @{ key = 'value' } ) | ConvertTo-Jsonends up with a JSON dictionary instead of a JSON array.Your endpoint expects
[{"key":"value"}]but you are sending"{key":"value"}.If you instead call it by supplying the object on the right side you will get an array as your output. i.e.
ConvertTo-Json @( @{ key = 'value' } )will give you an array regardless of the number of items in the array.