2

当我向 twilio API 发送响应时,我面临 415 不支持的媒体类型问题。

我正在使用 twilio API 向一个号码发送消息,如下所示

    TwilioRestClient client = new TwilioRestClient(accountSID, authToken);
    Account account = client.getAccount();
    MessageFactory messageFactory = account.getMessageFactory();
    List<NameValuePair> params = new ArrayList<NameValuePair>();
    params.add(new BasicNameValuePair(TO, mobileNumber));
    params.add(new BasicNameValuePair(FROM_, from));
    params.add(new BasicNameValuePair(BODY, messageBody));
    params.add(new BasicNameValuePair(CALL_BACK_URL, callBackUrl));

我为 twilio 配置了 callBackUrl,以便在我的消息状态发生变化时将响应发送到我的系统。

下面是处理 twilio 入站响应的代码

@POST
@Path("/callback/inbound/Twilio")
@Produces({MediaType.APPLICATION_JSON,MediaType.APPLICATION_XML,MediaType.TEXT_PLAIN })
@Consumes({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
public Response inboundTwilio(@QueryParam("AccountSid") String accountSid,
                              @QueryParam("MessageSid") String gatewayTxnId,
                              @QueryParam("MessageStatus") String messageStatus,
                              @QueryParam("ErrorCode") String errorCode,
                              @QueryParam("From") String from,
                              @QueryParam("To") String to,
                              @Context HttpServletRequest request) throws DateParseException {
    Response response = null;
    try{
        Long startTime = System.currentTimeMillis();
        LOGGER.info("In twilio inbound response for gatewayTxnId :: "+gatewayTxnId);
       // business logic and processing of twilio inbound handled here and it will return a status object
        LOGGER.info("Execution completed in :: "+(System.currentTimeMillis()-startTime));
        if (status != null && SUCCESS.equalsIgnoreCase(status)) {
            TwiMLResponse twiml = new TwiMLResponse();
            Message message = new Message("SUCCESS");
            twiml.append(message);
            response = Response.status(Response.Status.OK).entity(twiml).build();
        } else {
            response = Response.status(Response.Status.BAD_REQUEST).build();
        }
    }catch(Exception e){
        LOGGER.error("An error occured in MessageResource while processing inboundTwilio :: ",e.getMessage());
        response = Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(responseUtil.prepareResponse("500",
                        "Error occoured while processing your request", ExceptionUtils.getStackTrace(e)).toString()).build();
    }
    return response;
}

在处理来自 twilio 的请求后,将响应发送回它。当我在 twilio 调试器中检查我的响应状态时,它显示失败并出现 415 媒体不支持错误。我无法找到确切的原因。如果有任何问题,请指导我

根据我对https://www.twilio.com/docs/api/twiml/sms/your_response## Heading ##的理解开发

这就是典型的 Twilio 入站的样子

请求
URL
/rest/message/callback/inbound/Twilio?
参数
MESSAGESTATUS sent
APIVERSION 2010-04-01
SMSSID
SMSSTATUS sent
to +91123456789
FROM SENDER
MESSAGESID
ACCOUNTSID

4

1 回答 1

1

我相信您只需要像这样设置TwiML 响应的内容类型

response.setContentType("application/xml");
于 2016-08-01T20:53:51.743 回答