Powershell Invoke-Restmethod w/ JSON Body

0

Hope someone can help.

I’m trying to send a REST POST message using the Invoke-Rest method command to a sms service. When I use postman to send the request I’m able to send the message. However, when I try to do it in powershell I keep getting the following error and I’m unable to figure it out:

Invoke-RestMethod : {"error":"Validation error"}
At C:UsersMTROTTOneDrive - Volvo CarsProjectsSMS APImike.ps1:15 char:1
+ Invoke-RestMethod -Uri "$url" -Method Post -Body $mike -ContentType " ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebException
    + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand

I’m using the below code:

$url = "https://1234.com/api/send"

# Define the JSON body of the POST request
$body_json = [ordered]@{
    "apikey" = "KEY"
    "from" = "FRNR"
    "to" = "TONR"
    "message" = "test"
    "twoway" = "false"
    "conversation" = "CONV123"
    "checknumber" = "true"
} | ConvertTo-Json

Invoke-RestMethod -Uri $url -Method Post -Body $body_json -ContentType "application/json"

Kinda new to this so I would appreciate any help I can get.

Let me know if you need more info8 🙂

I’ve tried Invoke-Webrequest but still the same result.