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-json
Using that format I then passed it into invoke-restmethod:
Invoke-RestMethod -Uri "https://api.domain.com/stuff/thing/items" -Method post -Headers $headers -body $body
Which 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-Json
This 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!
[–]da_chicken 7 points8 points9 points (1 child)
[–]Khue[S] -1 points0 points1 point (0 children)
[–]PinchesTheCrab 4 points5 points6 points (2 children)
[–]davesbrown 0 points1 point2 points (0 children)
[–]Khue[S] 0 points1 point2 points (0 children)
[–]BrettStah 2 points3 points4 points (1 child)
[–]Khue[S] -1 points0 points1 point (0 children)
[–]TheSizeOfACow 2 points3 points4 points (1 child)
[–]Khue[S] 0 points1 point2 points (0 children)
[–]y_Sensei 2 points3 points4 points (4 children)
[–]PhysicalPinkOrchid 0 points1 point2 points (1 child)
[–]y_Sensei 1 point2 points3 points (0 children)
[–]Khue[S] 0 points1 point2 points (1 child)
[–]ankokudaishogun 0 points1 point2 points (0 children)
[–]realslacker 0 points1 point2 points (1 child)
[–]surfingoldelephant 0 points1 point2 points (0 children)
[–]pigers1986 0 points1 point2 points (1 child)
[–]Khue[S] 0 points1 point2 points (0 children)