Question

Bulk SMS Production App getting 400 bad request using powershell

  • 7 January 2022
  • 4 replies
  • 767 views

I think I have something wrong with the formatting of the body. this is my code and my error


Invoke-RestMethod : The remote server returned an error: (400) Bad Request.

At C:PowerShellTxt_Bulk.ps1:38 char:16



$Token = Get-RingCentralAuthentication

$Bearer = $Token.access_token

$Header = @{"accept" = 'application/json'

"authorization" = "Bearer $Bearer"

}


$AuthAPI = @{"endpoint" = 'https://platform.ringcentral.com'

"url" = '/restapi/v1.0/account/~/a2p-sms/batches' }

$AuthBodyTxt = @{

"from" = '+14085555555'

"text" = 'Hello Team testing API, no need to reply'

"messages" = @{"to" = '+18315555555' }

}

$UriTxt = $AuthAPI.endpoint + $AuthAPI.url

$requesttxt = Invoke-RestMethod -Method Post -Uri $UriTxt -Headers $Header -Body $AuthBodTxt

return $requesttxt


4 replies

Userlevel 1

The "messages" is an array of object and the "to" number is also an array of phone numbers. Right now, only one phone number per "to" array as we don't support group message yet.

"messages": [
    {
      "to": ["+14155550100"],
      "text": "Hello Alice"
    },
    {
      "to": ["+12125550100"],
      "text": "Hello Bob"
    }
  ]

https://developers.ringcentral.com/guide/messaging/sms/high-volume/sending-highvolume-sms#http

Thanks for the reply , I think Im having trouble because Powershell ConvertTo-JSON is not adding the square brackets.

How do I make my code formatted like yours?



$AuthAPITxt = @{"endpoint" = "https://platform.ringcentral.com"

"url" = "/restapi/v1.0/account/~/a2p-sms/batches" }

$AuthBodyTxt = @{"from" = "+14085555555";

"messages" = @{

"text" = "Hello Team testing";

"to" = "+18315555555"

}

}

$UriTxt = $AuthAPITxt.endpoint + $AuthAPITxt.url

$requesttxt = Invoke-RestMethod -Method post -Uri $UriTxt -Headers $Header -Body ($AuthBodyTxt|ConvertTo-JSON)>

when I test the code output I get this, will not having the square brackets cause the 400 request?
$AuthBodyTxt | ConvertTo-JSON

{

"messages": {

"text": "Hello Team testing",

"to": "+18315555555"

},

"from": "+14085555555"

}

I have figured out how to format the code, now Im getting a (503) Server Unavailable error. I am using the phone number that was provisioned for bulk SMS

 $AuthBodyTxt   = @{"from"     = "+14085555555";
                    "messages" = @(@{         
                                "text"     = "Hello Team testing";
                                "to" = @("+18315555555")
                                },
                                @{         
                                "text"     = "Hello Team testing";
                                "to" = @("+18315555555")
                                })
                    }

ConvertTo-Json -InputObject $AuthBodyTxt -Depth 3

{
    "messages":  [
                     {
                         "text":  "Hello Team testing",
                         "to":  [
                                    "+18315555555"
                                ]
                     },
                     {
                         "text":  "Hello Team testing",
                         "to":  [
                                    "+18315555555"
                                ]
                     }
                 ],
    "from":  "+14085555555"
}
Userlevel 1

What is your app client id?

BTW, If I were you, I would install one of the RC SDKs in a language that I know and can run on my local machine. Then copy paste the quick start code to run a quick test to make sure that my number and the app are setup correctly before trying own code like that.

Reply