Reputation: 653
I am having trouble converting a working cURL command in Windows to an equivalent PowerShell InvokeRestMethod command. It looks like I am pretty close. I am getting a response. However, the API doesn't seem to understand the nested hash table element "domain" sent by InvokeRestMethod command. All other hash elements seem to be recognized by the API just fine.
cURL command (working)
curl "https://api.rebrandly.com/v1/links" -X POST -H "apikey: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" -H "Content-Type: application/json" -d "{ \"destination\": \"http://longurl.com\", \"domain\": {\"fullName\": \"link.domain.com\"}}"
PowerShell:
$body = @{
"destination"="longurl.com"
"domain" = @("fullName", "link.domain.com")
} | ConvertTo-Json
$header = @{
"apikey"="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
"Content-Type"="application/json"
}
Invoke-RestMethod -Uri "https://api.rebrandly.com/v1/links" -Method 'Post' -Body $body -Headers $header
PS: I also tried using the below syntax. Unfortunately, I get the same result.. where the "domain" hash element is ignored.
$body = @{
"destination"="longurl.com"
"domain[0]" = "fullName"
"domain[1]" = "link.domain.com"
} | ConvertTo-Json
Upvotes: 1
Views: 195
Reputation: 13453
When you use @(...)
brackets it is interpreted as an array and not as a key-value hashtable, which would be converted into an [...]
array in JSON. Simply replace the brackets in the nested statement with the @{...}
curly brackets to indicate a hashtable:
$body = @{
"destination"="longurl.com"
"domain" = @{"fullName" = "link.domain.com"}
} | ConvertTo-Json
Which results in the matching JSON:
{
"destination": "longurl.com",
"domain": {
"fullName": "link.domain.com"
}
}
Upvotes: 2