2
votes

I am facing 415 unsupported media type issue when i send a response to twilio API.

I am sending a message to a number using twilio API as below

    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));

I configured the callBackUrl for twilio to send the response to my system when ever there is a change in the status of my message.

Below is the code that handles the twilio inbound response

@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;
}

After processing the request from twilio sending the response back to it.When i check the status of my response in twilio debugger,it is showing failed with 415 media unsupported error. I am unable to find the exact rool cause. Guide me if anything is wrong

Developed based on my understanding from https://www.twilio.com/docs/api/twiml/sms/your_response## Heading ##

This how a typical Twilio inbound looks like

Request
URL
/rest/message/callback/inbound/Twilio?
Parameters
MESSAGESTATUS sent
APIVERSION 2010-04-01
SMSSID
SMSSTATUS sent
TO +91123456789
FROM SENDER
MESSAGESID
ACCOUNTSID

1

1 Answers

1
votes

I believe you just need to set the content-type for TwiML response like so:

response.setContentType("application/xml");