function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Abhishek Singh 88Abhishek Singh 88 

System.HttpResponse[Status=BAD REQUEST, StatusCode=400] error i am getting while using sms api to send sms

Hi Developers.

i have writtent few code to test that whether sms api is working properly.Here is my code.
 
String username = '************' ;
            String password = '**********************';
            String message= 'Hi this msg. is for testing of "sending msg.on mobile when SLA breach"';
             HttpRequest req = new HttpRequest();
            req.setEndpoint('https://api.us1.corvisa.io/sms/');
            req.setMethod('POST'); 
            String phonenumber='+15123355101';
           "+15123355101"}';
            
            String body= '[{"from_number": "","messages": [{"text":  "text of message","to_number": "+15551239876"}]}]';
            //String jsonbody= json.serializePretty(body);
            Blob headerValue = Blob.valueOf(username + ':' + password);
            String authorizationHeader = 'Basic ' +  EncodingUtil.base64Encode(headerValue);
                 req.setHeader('Authorization', authorizationHeader);
                 req.setHeader('Content-Type','application/json');
                 HTTPResponse res= new HTTPResponse();
                 req.setBody(body); 
                 system.debug(req.getBody());
                 //req.setBody(jsonbody);
                 Http http = new Http();
                 //res.setStatusCode(200);
                 res = http.send(req);
                 system.debug(res.toString());
                  system.debug(res.getBody());

the format of body i copied from respective vendor's portal. format is mention like.
[
    {
        "from_number": "+15554447001",
        "messages": [
            {
                "text":  "text of message",
                "to_number": "+15551239876",
                
            }
        ]
    }
]

But when i looked at debug log is showing some error.
User-added image


Any suggestion will be appriciated.
rajat Maheshwari 6rajat Maheshwari 6

Hi Abhishek,

please use this syntax,- :

req.setBody(''
                    + 'From=+15554447001'
                    + '&To=+15551239876'
                    + '&Body=Hi,How are you'
                  );


Let me know, in case of any help
Thanks

rajat Maheshwari 6rajat Maheshwari 6

Hi Abhishek,

Please mark as best answer, if it works for you :)

Thanks

rajat Maheshwari 6rajat Maheshwari 6

Hi Abhishek,

Did you get a chance to look over the solution, which i have suggested ?

Thanks