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:\Users\MTROTT\OneDrive - Volvo Cars\Projects\SMS API\mike.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.

2
  • 1
    Aren't you missing authentication in your header? This API allows anonymous POST requests? Commented Feb 7, 2024 at 20:24
  • Does the API come with any documentation about what the request (esp the body) should look like? Your error {"error":"Validation error"} implies the request is reaching the API endpoint but it's returning a custom error message in the response body - possibly due to bad data. Without more detail about what the API expects it's hard to suggest what might be wrong with your request. If you have a working curl.exe command or similar please add the details of that to your question... Commented Feb 7, 2024 at 22:08

1 Answer 1

0

I'd assume it's linked to the way you handle your body, and you don't have any headers to send your query

Also don't believe you need a ordered hashtable to do that

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

$headers = @{
    "Authorization" = "your token if you need to use it"
    "Content"       = 'application/json' # If the content type needs to be setup in the header instead. Otherwise you can delete this line
}

$body = @{
    apikey       = "KEY"
    from         = "FRNR"
    to           = "TONR"
    message      = "test"
    twoway       = "false"
    conversation = "CONV123"
    checknumber  = "true"
} 

$params = @{
    Method      = 'POST'
    Uri         = $url
    headers     = $headers
    Body        = $body | ConvertTo-Json
    ContentType = 'application/json'
}

Invoke-RestMethod @params

PS: I would also confirm where you need to setup your token. Most likely not in the body, but in the header. Check the documentation of your api, you feel free to provide it in comments below

Sign up to request clarification or add additional context in comments.

2 Comments

I don’t think Invoke-RestMethod has a parameter called -params - did you mean to splat with Invoke-RestMethod @params?
Indeed sorry, I'm used to a custom cmdlet for rest-method I'm using. Editing

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.