I am trying to invoke the API using this request. This request is working fine in Postman. But how can I convert it to PowerShell Invoke-RestMethod
?
This is what I tried.
$token = "abcdef123456"
$url = "https://example.com/OVAV1/triggercalls/1"
$header = @{"Authorization"='abdcdef123456'}
$body =
{
"requestid": "<Echo back field>",
"templatename": "Line_Down",
"templateid": "abcde12345",
"noofrecords": 1,
"variableinfo": [
{
"<<MOBILE_NUMBER>>": "123456789",
"<<UNIQUE_CALL_IDENTIFIER>>": "A001",
"<<LinkType>>": "PRI",
"<<HostName>>": "HOSTNAME"
}
]
}
$request = Invoke-RestMethod -Uri $url -Headers $header -Body $body -ContentType "application/json" -Method Post
$request
I tried the JSON code in Postman. It's working fine. How can I use the same JSON code in PowerShell?
You can't put JSON data into PowerShell code like that. Put it in a here-string and it should work:
$body = @"
{
"requestid": "<Echo back field>",
"templatename": "Line_Down",
"templateid": "abcde12345",
"noofrecords": 1,
"variableinfo": [
{
"<<MOBILE_NUMBER>>": "123456789",
"<<UNIQUE_CALL_IDENTIFIER>>": "A001",
"<<LinkType>>": "PRI",
"<<HostName>>": "HOSTNAME"
}
]
}
"@
You could also define the data as a hashtable and convert it to a JSON string:
$data = @{
"requestid" = "<Echo back field>"
"templatename" = "Line_Down"
"templateid" = "abcde12345"
"noofrecords" = 1
"variableinfo" = @(
@{
"<<MOBILE_NUMBER>>" = "123456789"
"<<UNIQUE_CALL_IDENTIFIER>>" = "A001"
"<<LinkType>>" = "PRI"
"<<HostName>>" = "HOSTNAME"
}
)
}
$body = $data | ConvertTo-Json
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With