List to JSON for sending SMS

Hi,

I’m trying to add an SMS alert to a script using ClickSend however having an issue with getting powershell to output the required format in JSON i think, i get a response saying “The remote server returned an error: (400) Bad Request”

I suspect the issue is me not correctly formating the body of the message inline with their docs here - https://developers.clicksend.com/docs/rest/v3/#ClickSend-v3-API-SMS

 

$pwd = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f 'username', 'password')))



$body = @{
	messages = @{
		from = 'sender'
		to   = '+447800000000'
		body = 'SMS message body'
	}
	
}


$json = ConvertTo-Json -InputObject $body


$smsresponse = Invoke-RestMethod -Headers @{ Authorization = ("Basic {0}" -f $pwd) } -uri 'https://rest.clicksend.com/v3/sms/send' -Method Post -Body $json -ContentType 'application/json'

 

Any help adjusting my script would be much appreciated.

Regards,

Jamie

I have zero experience with this API, but all the examples I saw in the docs show the message property as an array. That is the only difference I see in your json. You might want to try that and see if it is the issue. Pretty simple to do, just wrap your messages value in an array subexpression @().

$body = @{
 messages = @(@{
 from = 'sender'
 to   = '+447800000000'
 body = 'SMS message body'
 })
 
}
 
 
$json = ConvertTo-Json -InputObject $body

 

 

Thank you so much, that worked!

I can’t believe how close my code was all this time, i actually swapped @{ for @( and played with different variations of that etc but never tried them together, although i now see whats happening and how that works so thank you very much.

 

  • Jamie