1

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

1 Answer 1

2

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"
               }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you!! I had tried the brackets earlier. However, I was still using a comma instead of an equal sign.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.