webworm
webworm

Reputation: 11019

Twilio XML response to SMS

I am trying to complete a sample SMS app with Twilio where I send a SMS message to my Twilio number and the Twilio service sends me back a reply. I know the Twilio service is reaching my API because I can see the incoming request from Twilio reaching my API and I can see the response of my API, but I think somethng is wrong, as I never get an SMS response back.

[HttpPost]
[Route("EchoTest")]
public IHttpActionResult EchoTest()
{
    string response = "<Response><Sms>Thanks for the message</Sms></Response>";
    return ResponseMessage(Request.CreateResponse(HttpStatusCode.OK, response, new XmlMediaTypeFormatter()));
}

I am returning the ResponseMessage so I can be consistent in returning an IHttpActionResult. I have also tried returning just HttpResponseMessage as seen below with the same results.

[HttpPost]
[Route("EchoTest")]
public HttpResponseMessage EchoTest()
{
    string response = "<Response><Sms>Thanks for the message</Sms></Response>";
    Request.CreateResponse(HttpStatusCode.OK, response, new XmlMediaTypeFormatter());
}

This is what my API is sending back ...

<string
 xmlns="http://schemas.microsoft.com/2003/10/Serialization/">&lt;Response&gt;&lt;Sms&gt;Thanks for the message&lt;/Sms&gt;&lt;/Response&gt;
</string>

Am I messing up the XML response? What I am looking to send back to Twilio is this ...

<Response><Sms>Thanks for the message</Sms></Response>

Upvotes: 2

Views: 581

Answers (1)

jdweng
jdweng

Reputation: 34421

See webpage : https://msdn.microsoft.com/en-us/library/hh969056(v=vs.118).aspx Try this

XElement response = XElement.Parse("<Response><Sms>Thanks for the message</Sms></Response>");
    Request.CreateResponse<XElement>(request, HttpStatusCode.OK, response);

Upvotes: 3

Related Questions