Output Type Specification via Accept Headers or Query Parameter

What type of output is returned from a request is determined by several rules:

  • format query string or post value that equals xml or json
  • Accept HTTP header on the client request
  • Default format if neither applies is JSON
  • Using a special return type like Stream, byte[] or Bitmap

format Query String or POST value

To force output to a specific format you can add a &format=xml or &format=json parameter to the URL or in the POST data. Specifying this value overrides any other settings explicitly.

Accept HTTP Headers

Callback handler checks for the Accept http header in a request and looks for Json and XML content types in the header string. If it finds a JSON content type that's used. If it finds XML, that's used. JSON takes precendence always regardless of order of the headers.

A typical Accept header looks like this:

Accept: text/xml

Using a client like jQuery's AJAX method you can add a request header explicitly

$.ajax(
{
   url: url,
   data: postData,
   type: "POST",
      beforeSend: function (xhr) {
             xhr.setRequestHeader("Accept","application/json");
      }
});

Typically for JavaScript applications you can just omit the header since CallbackHandler's default response output type if none is specified is application/json.

If you're using a desktop .NET application you might use WebClient:

var http = new WebClient();
http.Headers.Add("Accept","text/xml");
string xml = http.DownloadString(url);


© West Wind Technologies, 1996-2016 • Updated: 12/19/15
Comment or report problem with topic