无法通过 twilio 发送短信,收到 400 错误

我正在尝试使用 Java 代码中的 HttpEntity 通过 Twilio 发送短信。我收到了 400 错误,但没有任何详细信息。下面是响应内容:

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

以下是我用于发送请求的代码。在 Postman 中,请求成功。我不能使用 Java SDK,必须使用 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;

认证与 Twilio 成功。因此没有身份验证问题。我不确定请求到底有什么问题。

点赞
用户997251
用户997251

如果您的 HTTP 请求路径没有以 '/' 开头,那么您可能会遇到此错误。

2022-02-09 10:26:14