News & Announcements User Community Developer Community

Welcome to the RingCentral Community

Please note the community is currently under maintenance and is read-only.

Search
Make sure to review our Terms of Use and Community Guidelines.
  Please note the community is currently under maintenance and is read-only.
Home » Developers
Bulk SMS Production App getting 400 bad request using powershell
Tags: high-volume sms
Jan 7, 2022 at 1:40pm   •   3 replies  •  0 likes
ken-nye

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:\PowerShell\Txt_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

3 Answers
answered on Jan 8, 2022 at 10:11am  

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


 0
answered on Jan 8, 2022 at 2:36pm  

I can use get and retrieve my messages I sent using the web interface, so I know that my credentials are correct. App client id 0BeUP9IIQCSuO-LdU9dXQw



 0
answered on Jan 8, 2022 at 10:26am  

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"

}


 0
answered on Jan 8, 2022 at 11:52am  

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"
}

 0
on Jan 8, 2022 at 1:52pm   •  0 likes

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.

on Jan 8, 2022 at 2:37pm   •  0 likes

I can use get and retrieve my messages I sent using the web interface, so I know that my credentials are correct. App client id 0BeUP9IIQCSuO-LdU9dXQw

on Jan 8, 2022 at 2:58pm   •  0 likes

I see 2 numbers were provisioned for HV SMS. (xxx) xxx-1306 assigned to extension 103, and (xxx) xxx-9720 assigned to extension 102. Which user do you login your app and which number do you use to send messages? It must be the owner of a number who can send messages from that number. So check and authenticate the app and use the number accordingly.


on Jan 8, 2022 at 7:29pm   •  0 likes

I'm using the 1306 number, I am able to GET and retrieve the batches that I used the Web interface to send some txt with. Doesn't that mean my credentials are correct?

id               : 97168a03-eff5-4c5d-b200-0438866c84fe
from             : +1******1306
batchSize        : 6
processedCount   : 6
status           : Completed
creationTime     : 2022-01-06T20:18:23.625423Z
lastModifiedTime : 2022-01-06T20:18:29.094971Z
rejected         : {}
cost             : 0.042
	

The one thing I'm not sending is the extension. I'm not sure where I would add that. I changed my grant type to have the extension, but it made no difference


    $AuthBody   = @{"grant_type"    = 'password'
                    "username"      = $SessionUser
                    "password"      = $SessionPassword
                    "extension"      = $SessionExtension }
on Jan 10, 2022 at 7:46am   •  0 likes

Can you try login using the -1306 number as the username? You can also specify the extension number 103 or just omit that. This is just for me to double check an issue, not a solution though.

Now, can you print the trace and get the requestId from the response header?

on Jan 10, 2022 at 8:02am   •  0 likes

I am using 1306 as the login, where should I specify the extension, as you can see in the code above, I added it to the Grant Type, but it made no difference. this is what I got back, I dont see a requestid

IsMutuallyAuthenticated : False
Cookies                 : {}
Headers                 : {Transfer-Encoding, Connection, RCRequestId, X-Rate-Limit-Group...}
SupportsHeaders         : True
ContentLength           : -1
ContentEncoding         : 
ContentType             : application/json
CharacterSet            : 
Server                  : nginx
LastModified            : 1/10/2022 8:00:38 AM
StatusCode              : ServiceUnavailable
StatusDescription       : 
ProtocolVersion         : 1.1
ResponseUri             : https://platform.ringcentral.com/restapi/v1.0/account/~/a2p-sms/batches
Method                  : POST
IsFromCache             : False
on Jan 10, 2022 at 8:05am   •  0 likes

The requestId is inside the headers array. So print the headers to get it.

on Jan 10, 2022 at 11:51am   •  0 likes

I have been trying all morning to get the headers, but it seems that the values are not there, or at least Im not finding them

I tried both Invoke-WebRequest and Invoke-RestMethod

 try{$requesttxt =   Invoke-WebRequest    -Method post  -Uri $UriTxt  -Headers $Header -Body ($AuthBodyTxt|ConvertTo-JSON -Depth 3)} catch{
 
$headerResponse =  $_.Exception.Response.headers
foreach ($h in $headerResponse) {
    Write-Host " "
}
 }

returns this

Transfer-Encoding
Connection
RCRequestId
X-Rate-Limit-Group
X-Rate-Limit-Limit
X-Rate-Limit-Remaining
X-Rate-Limit-Window
RoutingKey
Retry-After
Content-Type
Date
Server 
on Jan 10, 2022 at 12:09pm   •  0 likes

You have to capture and print out the value of "RCRequestId". The header is on your list above. I don't know how to get it in powershell but you must be the expert.

BTW, You wrote that you could send messages using the Web interface, did you mean the "Try it out" from the API reference or from this app.

on Jan 10, 2022 at 12:56pm   •  0 likes

I was using the HighVolume Web App
I am by no means an expert, thats why I'm here asking for help, and thanks for all you have given. I feel like I'm very close to success. I have been trying to get the headers, but it seem as though Microsoft did not do a good job of keeping the header in Invoke-RestMethod when an error is thrown.

article here: Invoke-RestMethod should return the full error response from the remote endpoint

I wonder if there is any other way you can help without the RCRequestID. The only thing I notice is the inside the developer site, the Production Credentials Account number is our #800, not the #1306 . I don't know if that matters but is all I have left
ring-app-sms.png
thanks again

on Jan 10, 2022 at 1:37pm   •  0 likes

I need the request id so that I can have a look at the exact API call you made to trace the cause of the failure.

The production number shown there is your main company number. It's just a reminder that it's your production account.

What other programming language can you run on your local machine, Python, Node, PHP, Java, C#, Ruby? Just for testing.

on Jan 10, 2022 at 2:10pm   •  0 likes

The plot thickens. I upgraded my powershell from 5.x to 7.x, I got 2 txt messages through, and now I Im getting this error

   Invoke-RestMethod    -Method post  -Uri $UriTxt  -Headers $Header - …
   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  {"errorCode":"CMN-201","message":"Service Temporary
 Unavailable","errors":[{"errorCode":"CMN-201","message":"Service Temporary Unavailable"}]}

on Jan 10, 2022 at 2:21pm   •  0 likes

and powershell 7 gives the RCRequestID

	 Server: nginx
  Date: Mon, 10 Jan 2022 22:19:46 GMT
  Transfer-Encoding: chunked
  Connection: keep-alive
  RCRequestId: 6b1059bc-7263-11ec-a7ea-0050568c5686
  X-Rate-Limit-Group: light
  X-Rate-Limit-Limit: 50
  X-Rate-Limit-Remaining: 49
  X-Rate-Limit-Window: 60
  RoutingKey: IAD01P08
  Retry-After: 40
  Content-Type: application/json
	



on Jan 10, 2022 at 2:59pm   •  0 likes

Is this request id (RCRequestId: 6b1059bc-7263-11ec-a7ea-0050568c5686) for the one that failed with "Service Temporary Unavailable" CMN-201?

on Jan 10, 2022 at 3:04pm   •  0 likes

yes, Im getting that error everytime , now Im using Powershell 7, and an hour ago, I got 2 SMS successfully sent, now no more, so something was working for a min

on Jan 11, 2022 at 8:40am   •  0 likes

Hi Phong, any luck looking up the RCRequestID? I can successfully use the GET to retrieve my Batch IDs, so I feel confident that I have my credentials correct.

RCRequestID :  1849f4f6-72fc-11ec-84c0-0050568c6525
Date        :  Tue, 11 Jan 2022 16:32:40 GMT

id               : 9567d2b6-7184-4dde-b1a7-3a411fb1376a
from             : +14083531306
batchSize        : 2
processedCount   : 2
status           : Completed
creationTime     : 1/10/2022 9:33:42 PM
lastModifiedTime : 1/10/2022 9:33:47 PM
rejected         : {}
cost             : 0.014

now if I add the Body and change it to Post

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

I get the CMN-201 error

{"errorCode":"CMN-201","message":"Service Temporary Unavailable","errors":[{"errorCode":"CMN-201","message":"Service Temporary Unavailable"}]}

here is my last attempt at Post

Server: nginx
  Date: Tue, 11 Jan 2022 16:32:40 GMT
  RCRequestId: 17fe8f34-72fc-11ec-8e6e-0050568cee8a

Once again, thanks for any and all help

on Jan 11, 2022 at 8:51am   •  0 likes

Not yet, but we did not log the post body so I could not verify what could be wrong with that API call.

However, can you remove one of the recipients and send only to one recipient?

on Jan 11, 2022 at 9:21am   •  0 likes

Done, here is the body


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

and here is the RCRequestID

  Date: Tue, 11 Jan 2022 17:19:15 GMT
  RCRequestId: 9a3162d2-7302-11ec-a3f5-0050568c5686
	
on Jan 11, 2022 at 10:00am   •  0 likes

I don't ask for the request body from your end. But that is ok. Did this call succeed or fail?

on Jan 11, 2022 at 10:34am   •  0 likes

no, it failed with the same error

on Jan 11, 2022 at 12:54pm   •  0 likes

This is weird but there is a reason for it. For now, can you try again and put the "from" param before the "messages" param.

There will be some fixes to remove this limitation later.

on Jan 11, 2022 at 1:07pm   •  0 likes

I tried, but it seems that Microsoft orders it that way no matter how I order it

When I convert to JSON in powershell like this

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

ConvertTo-Json -InputObject $AuthBodyTxt -Depth 3

it creates an output with From at the bottom like this

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

I cant figure out why Powershell always put from a the bottom, maybe because messages is a array?

on Jan 11, 2022 at 1:08pm   •  0 likes

I the just wrote a string to look like the JSON and use that string as the body, instead of using convert to JSON, like this


$testMessage = "{
    $([char]34)from$([char]34): $([char]34)+14085555555$([char]34),
    $([char]34)messages$([char]34): [
      {
        $([char]34)text$([char]34): $([char]34)Hello Team $(Get-date)$([char]34),
        $([char]34)to$([char]34): [
            $([char]34)+18315555555$([char]34)
        ]
      }
    ]
  }"
	

this returns this

{
    "from": "+14085555555",
    "messages": [
      {
        "text": "Hello Team 01/11/2022 11:18:12",
        "to": [
            "+18315555555"
        ]
      }
    ]
  }
	

and this works!

on Jan 11, 2022 at 1:25pm   •  0 likes

Thanks for confirmation! Meanwhile, this may help you to convert.

on Jan 11, 2022 at 1:34pm   •  0 likes

thanks.. its those little things.. so for the record, here is the correct code. just needed [pscustomobject] in front


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


$response =  Invoke-RestMethod  -Method post  -Uri $UriTxt  -Headers $Header -Body ($AuthBodyTxt|ConvertTo-JSON -Depth 3)
on Jan 10, 2022 at 11:42am   •  0 likes

I have been trying all morning to get the headers, but it seems that the values are not there, or at least Im not finding them

I tried both Invoke-WebRequest and Invoke-RestMethod

 try{$requesttxt =   Invoke-WebRequest    -Method post  -Uri $UriTxt  -Headers $Header -Body ($AuthBodyTxt|ConvertTo-JSON -Depth 3)} catch{
 
$headerResponse =  $_.Exception.Response.headers
foreach ($h in $headerResponse) {
    Write-Host $h
}
 }

returns this

Transfer-Encoding
Connection
RCRequestId
X-Rate-Limit-Group
X-Rate-Limit-Limit
X-Rate-Limit-Remaining
X-Rate-Limit-Window
RoutingKey
Retry-After
Content-Type
Date
Server 



A new Community is coming to RingCentral!

Posts are currently read-only as we transition into our new platform.

We thank you for your patience
during this downtime.

Try Workflow Builder

Did you know you can easily automate tasks like responding to SMS, team messages, and more? Plus it's included with RingCentral Video and RingEX plans!

Try RingCentral Workflow Builder

PRODUCTS
RingEX
Message
Video
Phone
OPEN ECOSYSTEM
Developer Platform
APIs
Integrated Apps
App Gallery
Developer support
Games and rewards

RESOURCES
Resource center
Blog
Product Releases
Accessibility
QUICK LINKS
App Download
RingCentral App login
Admin Portal Login
Contact Sales
© 1999-2024 RingCentral, Inc. All rights reserved. Legal Privacy Notice Site Map Contact Us