Reputation: 4573
I'm building a Web API 2 API to respond to Twilio webhooks. I've built a fairly standard endpoint, and I can see that Twilio hits it when I text my number there, but it's getting a 12300 - Invalid Content-Type - Invalid Content-Type: application/json; charset=utf-8 supplied.
I found that Twilio requires XML in response, but uses an accept-header of */*, so my endpoint is defaulting to return JSON.
[Route("reply")]
[HttpPost]
public TwiMLResult TwilioSMS([FromBody]SmsRequest request)
{
var requestBody = request.Body;
var response = new MessagingResponse();
if (requestBody == "hello")
response.Message("Hi!");
else
response.Message("Invalid Input");
return new TwiMLResult(response);
}
I've tried returning an HttpResponseMessage instead (as below), so I can specify XML, but I get a 12200 Schema Validation Warning - cvc-elt.1: Cannot find the declaration of element 'TwiMLResult' from Twilio instead.
HttpResponseMessage resp = Request.CreateResponse<TwiMLResult>(
HttpStatusCode.OK, value: new TwiMLResult(response),
formatter: Configuration.Formatters.XmlFormatter);
How can I force this controller/endpoint to return XML, but not affect my other controllers/endpoints (which should still return based on accept-header)?
Upvotes: 1
Views: 1078
Reputation: 10366
Twilio developer evangelist here.
Try returning the MessagingResponse
directly using a XmlMediaTypeFormatter
:
return this.Request.CreateResponse(
HttpStatusCode.OK, XElement.Parse(response), new XmlMediaTypeFormatter());
Hope that helps.
Upvotes: 3