【问题标题】:Sms cannot be Sent via twilio getting 400 error无法通过 twilio 发送短信,出现 400 错误
【发布时间】:2021-05-01 23:41:48
【问题描述】:

我正在尝试使用我的 Java 代码中的 HttpEntity 通过 Twilio 发送 SMS。我收到 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 验证成功。所以没有身份验证问题。我不确定请求到底有什么问题。

【问题讨论】:

  • 您得到的错误在正文中显示“openresty”。这似乎是向 Twilio 发出请求的过程中的事情,但这不是 Twilio 的回应。您是否有一个必须传递请求的代理,并且您是否通过该代理正确发送请求?

标签: twilio sms resttemplate httpentity


【解决方案1】:

如果您的 HTTP 请求中的路径不以“/”开头,您可能会收到此错误。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-30
    • 1970-01-01
    相关资源
    最近更新 更多