Sms cannot be Sent via twilio getting 400 error

606 views Asked by At

I am trying to send SMS via Twilio using HttpEntity from my Java code. I am getting 400 Bad Request with no details. The response is below.

 [<html>
 <head><title>400 Bad Request</title></head>
 <body>
 <center><h1>400 Bad Request</h1></center>
 <hr><center>openresty</center>
 </body>
 </html>]

Below is the code I have used to send a request. The request is successful from Postman. I cannot use Java SDK and has to use RestTemplate.

 HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
        headers.add("route-to-back-end-endpoint", "https://api.twilio.com/2010-04-01/Accounts/xxx/Messages.json");   
        MultiValueMap<String, String> map = new LinkedMultiValueMap<>();
        map.add("To", "+91*******");
        map.add("From", "+1******");
        map.add("Body", "Try try try");   
        HttpEntity<MultiValueMap<String, String>> entity = new HttpEntity<>(map, headers);   
        String response = null;
        try {
            restTemplateSsl.getInterceptors().add(new BasicAuthenticationInterceptor("xxx", "yyy"));
            response = restTemplateSsl.postForObject("Internal Xml Gateway Link", entity, String.class);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("Exception : {} ", e.getMessage());
        }
        log.info("Response Received :{}", response);
        return response;

The authentication is successful with Twilio. So no authentication issue. I am not sure what is exactly wrong in request.

1

There are 1 answers

0
Florian Loitsch On

You might get this error if the path in your HTTP request doesn't start with '/'.