Welcome › Forums › General PowerShell Q&A › List to JSON for sending SMS
- This topic has 2 replies, 2 voices, and was last updated 1 week, 2 days ago by
Participant.
-
AuthorPosts
-
-
January 11, 2021 at 4:17 am #285115
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
PowerShell123456789101112131415161718$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
-
This topic was modified 1 week, 2 days ago by
JDobbsy1987.
-
This topic was modified 1 week, 2 days ago by
JDobbsy1987.
-
This topic was modified 1 week, 2 days ago by
JDobbsy1987.
-
This topic was modified 1 week, 2 days ago by
JDobbsy1987. Reason: Code E
-
This topic was modified 1 week, 2 days ago by
JDobbsy1987. Reason: Code Formatting
-
This topic was modified 1 week, 2 days ago by
kvprasoon.
-
This topic was modified 1 week, 2 days ago by
-
January 11, 2021 at 12:04 pm #285184
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 @().
PowerShell1234567891011$body = @{messages = @(@{from = 'sender'to = '+447800000000'body = 'SMS message body'})}$json = ConvertTo-Json -InputObject $body -
January 11, 2021 at 2:42 pm #285226
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
-
-
AuthorPosts
- You must be logged in to reply to this topic.