Gutemberg Ribeiro Gutemberg Ribeiro - 26 days ago 23
C# Question

Return XML content on WebAPI OWIN for Twilio

I'm making a pretty simple WebAPI using OWIN/Katana self-hosted in a Azure Worker role. Everything is fine from the host perspective since I receive the request and it is routed to my Action just fine.

The problem is that the action MUST return a XML for the API caller/invoker and it is return a wrong encoding string as follow:

RAW Response:

HTTP/1.1 200 OK
Content-Length: 150
Content-Type: text/xml; charset=utf-8
Server: Microsoft-HTTPAPI/2.0
Date: Wed, 16 Jul 2014 05:49:42 GMT

<string xmlns="http://schemas.microsoft.com/2003/10/Serialization/">&lt;Response&gt;
&lt;Say&gt;Hello World&lt;/Say&gt;
&lt;/Response&gt;</string>

RAW Request:


GET http://localhost:81/v1/ivr/menu?from=+12345&to=645645&callsid=11111111 HTTP/1.1
User-Agent: Fiddler
Host: localhost:81
Content-Type: text/xml;

response.ToString() result:

<Response>
<Say>Hello World</Say>
</Response>

API Controller Action code:

[HttpGet]
[Route("menu")]
public IHttpActionResult Menu(string from, string to, string callSid)
{
var response = new TwilioResponse();
response.Say("Hello World");

return Ok(response.ToString());
}


Well, all I need is to return the XML as this:

<Response>
<Say>Hello World</Say>
</Response>


What exactly am I doing wrong? The response is coming between tags and with a weird xmlns with what seems to be a wrong encoding...

I've tried to add Content-Type and Accept headers on the request to text/xml(I can't use application/xml but even if I do, it don't changes the output response)...

Also I've tried to create a OWINMiddleware that force the Response content type to text/xml as this:

public class XmlResponseMiddleware : OwinMiddleware
{
public XmlResponseMiddleware(OwinMiddleware next) : base(next)
{

}

public override async Task Invoke(IOwinContext context)
{
context.Response.ContentType = "text/xml";
await this.Next.Invoke(context);
}
}


No lucky...

Any help would be appreciated.

Thanks!

Answer Source
[HttpGet]
[Route("menu")]
public HttpResponeMessage Menu(string from, string to, string callSid) 
{
     var response = new TwilioResponse();
     response.Say("Hello World");

    return new HttpResponseMessage()
    {
        Content = new StringContent(
            response.ToString(), 
            Encoding.UTF8, 
            "text/xml"
        )
    };
}