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"));
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
setting the request ContentType instead of the AcceptHeader did the trick:
request.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/atom+xml");
Method 2
Thanks to a search on the keywords request.Headers.Accept.Add and MediaTypeWithQualityHeaderValue that were presented by this question, I found a CodeProject Article that actually presented the syntax to correctly add the Accept header and solve the same issue:
this.Request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/atom+xml"));
instead of MediaTypeWithQualityHeaderValue.Parse("application/atom+xml") which throws the FormatException.
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0