Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WebAPI OData $format to xml

for my WebAPI OData application, im trying to give my client (browser) the decision what format the data output should be. as $format is not implemented yet in WebAPI OData, im using Raghuramn's example here: https://gist.github.com/raghuramn/5556691

var queryParams = request.GetQueryNameValuePairs();
var dollarFormat = queryParams.Where(kvp => kvp.Key == "$format").Select(kvp => kvp.Value).FirstOrDefault();

if (dollarFormat != null)
{
    request.Headers.Accept.Clear();
    request.Headers.Accept.Add(MediaTypeWithQualityHeaderValue.Parse(dollarFormat));

    // remove $format from the request.
    request.Properties[HttpPropertyKeys.RequestQueryNameValuePairsKey] = queryParams.Where(kvp => kvp.Key != "$format");
}

This works for JSON ($format=application/json;odata=fullmetadata) and JSON light (format=application/json;odata=light) but so far not for xml.

if i add $format=application/XML to the querystring, it still outputs to json light. how do i force XML output?

EDIT:

even if i force xml in Fiddler by sending Content-type: application/xml and Accept: application/xml with the request, the response simply lists: Content-Type: application/json; odata=minimalmetadata; streaming=true; charset=utf-8

EDIT 2:

Accept: application/atom+xml does seem to output xml in the raw response. Unfortunately, "application/atom+xml" throws a FormatException in:

request.Headers.Accept.Add(MediaTypeWithQualityHeaderValue.Parse("application/atom+xml"));
like image 491
stefjnl Avatar asked Feb 05 '14 10:02

stefjnl


1 Answers

setting the request ContentType instead of the AcceptHeader did the trick:

request.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/atom+xml");
like image 168
stefjnl Avatar answered Sep 22 '22 16:09

stefjnl