Language

Explore the OData Endpoint

By |

In this section, we’ll use the Fiddler Web Debugging Proxy to send requests to the endpoint and examine the response messages. This will help you to understand the capabilities of an OData endpoint. (If you just want to create a client application right way, skip this section and jump to Create a C# Client for the OData Endpoint. If so, you may want to come back to this section later.)

In Visual Studio, press F5 to start debugging. By default, Visual Studio opens your browser to http://localhost:port, where port is the port number configured in the project settings.

You can change the port number in the project settings. In Solution Explorer, right-click the project and select Properties. In the properties window, select Web. Enter the port number under Project Url.

Launch Fiddler. In the Composer tab, enter the following URI: http://localhost:port/odata/, where port is the port number.

Click the Execute button. Fiddler will send an HTTP GET request to your application. You should see the response in the Web Sessions list. If everything is working, the status code will be 200.

Double-click the response in the Web Sessions list to see the details of the response message in the Inspectors tab.

The raw HTTP response message should look similar to the following:

HTTP/1.1 200 OK
Cache-Control: no-cache
Pragma: no-cache
Content-Type: application/json; odata=minimalmetadata; streaming=true; charset=utf-8
Expires: -1
Server: Microsoft-IIS/8.0
DataServiceVersion: 3.0
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
Content-Length: 149

{
  "odata.metadata":"http://localhost:18285/odata/$metadata","value":[
    {
      "name":"Products","url":"Products"
    }
  ]
}

The response body contains the OData service document in JSON format. The service document contains an array of JSON objects that represent the entity sets. In this case, there is a single entity set, "Products". To query this set, send a GET request to http://localhost:port/odata/Products. The response should be similar to the following:

HTTP/1.1 200 OK
Cache-Control: no-cache
Pragma: no-cache
Content-Type: application/json; odata=minimalmetadata; streaming=true; charset=utf-8
Expires: -1
Server: Microsoft-IIS/8.0
DataServiceVersion: 3.0
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
Content-Length: 460

{
  "odata.metadata":"http://localhost:18285/odata/$metadata#Products","value":[
    {
      "ID":1,"Name":"Hat","Price":"15","Category":"Apparel"
    },{
      "ID":2,"Name":"Socks","Price":"5","Category":"Apparel"
    },{
      "ID":3,"Name":"Scarf","Price":"12","Category":"Apparel"
    },{
      "ID":4,"Name":"Yo-yo","Price":"4.95","Category":"Toys"
    },{
      "ID":5,"Name":"Puzzle","Price":"8","Category":"Toys"
    }
  ]
}

To query a product by key: http://localhost:port/odata/Products(1). The response:

HTTP/1.1 200 OK
Cache-Control: no-cache
Pragma: no-cache
Content-Type: application/json; odata=minimalmetadata; streaming=true; charset=utf-8
Expires: -1
Server: Microsoft-IIS/8.0
DataServiceVersion: 3.0
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
Content-Length: 150

{
  "odata.metadata":"http://localhost:18285/odata/$metadata#Products/@Element",
      "ID":1,"Name":"Hat","Price":"15","Category":"Apparel"
}

OData Formats

OData supports several formats:

  • Atom Pub (XML)
  • JSON “light” (introduced in OData v3)
  • JSON “verbose” (OData v2)

By default, the response uses JSON “light” format. However, a client can specify the format by setting the Accept header in the request.

Atom Pub

In Fiddler, click the Composer tab. In the Request Headers text box, type the following on a new line:

Accept: application/atom+xml

Send a GET request to http://localhost:port/odata/Products(1). The response should look similar to the following.

HTTP/1.1 200 OK
Cache-Control: no-cache
Pragma: no-cache
Content-Type: application/atom+xml; charset=utf-8
DataServiceVersion: 3.0
Content-Length: 1072

<?xml version="1.0" encoding="utf-8"?>
<entry xml:base="http://localhost:18285/odata/" 
    xmlns="http://www.w3.org/2005/Atom" 
    xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices" 
    xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" 
    xmlns:georss="http://www.georss.org/georss" 
    xmlns:gml="http://www.opengis.net/gml">
  <id>http://localhost:18285/odata/Products(1)</id>
  <category term="ProductService.Models.Product" 
    scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" />
  <link rel="edit" href="http://localhost:18285/odata/Products(1)" />
  <link rel="self" href="http://localhost:18285/odata/Products(1)" />
  <title />
  <updated>2013-01-27T16:41:36Z</updated>
  <author>
    <name />
  </author>
  <content type="application/xml">
    <m:properties>
      <d:ID m:type="Edm.Int32">1</d:ID>
      <d:Name>Hat</d:Name>
      <d:Price m:type="Edm.Decimal">15.00</d:Price>
      <d:Category>Apparel</d:Category>
    </m:properties>
  </content>

You can see one obvious disadvantage of the Atom format: It’s a lot more verbose than the JSON light. However, if you have a client that understands AtomPub, the client might prefer that format over JSON.

Verbose JSON

Version 3 of the OData protocol introduced the JSON light format. For backward compatibility, a client can request the older “verbose” JSON format. The media type is application/json;odata=verbose Here is the verbose version of a Product entity:

{
  "d":{
    "__metadata":{
      "id":"http://localhost:18285/odata/Products(1)",
      "uri":"http://localhost:18285/odata/Products(1)",
      "type":"ProductService.Models.Product"
    },"ID":1,"Name":"Hat","Price":"15.00","Category":"Apparel"
  }
}

This format conveys more metadata in the response body, which can add considerable overhead over an entire session. Also, it adds a level of indirection by wrapping the object in a property named “d”.

Mike Wasson

By Mike Wasson, Mike Wasson is a programmer-writer at Microsoft.