Question

So I'm working on a web API that a Roku Channel will interact with to send and receive data. Roku SDK has a built in XML Parser that is easy to use, but the only problem is that Roku will only parse XML wrapped in an <rsp stat="ok"></rsp> element. I don't see how or where to override the XML Output on the web API to wrap it with the <rsp> element.

So my question is, how can I override the XML Formatter and insert <rsp stat="ok"> before the output, and </rsp> after?

Was it helpful?

Solution

If you are ensuring that you will return only XML by removing JSON formatter like this

config.Formatters.Remove(config.Formatters.JsonFormatter);

you can use a message handler to add the envelope blindly for all responses like this.

public class MyHandler : DelegatingHandler
{
    protected override async Task<HttpResponseMessage> SendAsync(
                                       HttpRequestMessage request,
                                            CancellationToken cancellationToken)
    {
        var response = await base.SendAsync(request, cancellationToken);
        string responseBody = "<rsp stat=\"ok\">" + 
                                  await response.Content.ReadAsStringAsync() +
                              "</rsp>";
        response.Content = new StringContent(
                          responseBody, Encoding.UTF8, "application/xml");
        return response;
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top