Twilio API 415不支持媒体类型错误。错误 - 11200

时间:2016-07-30 20:57:53

标签: java twilio restful-url twilio-api

当我向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媒体不支持错误。我无法找到确切的rool原因。如果出现任何问题,请指导我

根据我对https://www.twilio.com/docs/api/twiml/sms/your_response##标题##

的理解而开发

这是典型的Twilio入站看起来像

请求
URL
/ REST /消息/回调/入站/ Twilio?
参数
MESSAGESTATUS发送了 APIVERSION 2010-04-01
SMSSID
SMSSTATUS发送了 至+91123456789
来自SENDER
MESSAGESID
ACCOUNTSID

1 个答案:

答案 0 :(得分:1)

我相信您只需设置content-type for TwiML response就像这样:

response.setContentType("application/xml");