What is OData?
The Open Data (OData) protocol is an internet protocol to query and update data over HTTP. The OData consumer or client sends a request to the server in the form of an URL with a query string parameter. The consumer or client will then receive a response in JavaScript Object Notation (JSON) and then interpret it. It is used to query and manipulate data that is exposed in a RESTful way.
Microsoft initially defined the term, but now it is being standardized by
OData is a successor of WCF Data Services 5.0 and can be treated as a particular form of REST. It allows several concepts like querying for data (using filters), adding new items, updating them, etc.
Anyone who wants to consume data from a service that exposes its data using the OData protocol can benefit from using OData.
Pros of OData protocol
Some benefits of using OData are listed below.
-
It supports several data source models (RDBMS, NoSQL Data Stores, CSV files, and more).
-
It supports querying a set of items from a service.
Many websites that expose their APIs via RESTful interfaces, e.g., YouTube, Google Drive, and Flickr use this protocol.
Cons of OData protocol
Some of the drawbacks of using OData are listed below.
-
It requires services to expose their APIs via RESTful interfaces.
-
It tends to become bloated over time, similar to WCF Data Services.
Examples
Below are some of the sample OData queries for your reference and to help understand the key capabilities of the OData protocol better.
1. Retrieving all the records
For example, the query below retrieves all the blog posts for this site.
GET http://example.com/posts?$select=PostID,Title
2. Using pagination in OData
GET http://example.com/posts?$orderby=PostedTime%20desc&$skip=100&$top=5
3. Filtering OData queries
GET
http://example.com/Posts?$filter=Category eq 'dotnet' or Category eq 'javascript'
4. Updating a record in OData
PUT http://example.com/Posts?$id=1&Title='new title'
5. Updating a record using the $expand option
PUT http://example.com/Posts?$expand=Author&Author.Name='joe'