Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best way in REST api to request a summary representation of a resource?

Tags:

rest

I have a REST api that deals with a large resource, customers. The client sometimes wants an abbreviated/summary representation of a customer. What would be a good way to specify that the request is for a summary representation?

To get a full representation for customer 123 its: /customers/123

To get a summary representation is /customers/123/summary a good way? Are there better options?

like image 697
BobJ Avatar asked Apr 12 '13 17:04

BobJ


People also ask

What is the most popular way to represent a resource in REST?

REST uses various representations to represent a resource where Text, JSON, XML. The most popular representations of resources are XML and JSON.

Which is the preferable format to expose data using RESTful?

Use JSON as the Format for Sending and Receiving Data This is because, with XML for example, it's often a bit of a hassle to decode and encode data – so XML isn't widely supported by frameworks anymore.

In which format can we represent a resource to be accessed via REST API?

When a client request is made via a RESTful API, it transfers a representation of the state of the resource to the requester or endpoint. This information, or representation, is delivered in one of several formats via HTTP: JSON (Javascript Object Notation), HTML, XLT, Python, PHP, or plain text.


1 Answers

To make your solution reusable and flexible, I would suggest implementing a "filter" query parameter. In there, you can put any fields required by the client:

GET /customers/123?fields=id,first_name,last_name,email

That way if later you need to create a different summary for a different task, you won't have anything to modify.

I wouldn't recommend /customers/123/summary because it's not flexible. It might be ok for one case, but if the client needs to access different properties for a different case, you will have to tweak the resource (most likely by returning more fields than needed). If you want to "hide" the field names, perhaps an alternative could be something like this:

GET /customers/123?view=DESCRIPTION

Where "DESCRIPTION" would describe the type of summary. For example:

GET /customers/123?view=addresses_only // eg. returns billing and home address
GET /customers/123?view=short // eg. returns only id, first name, last name

This is still flexible since you can easily create new views.

like image 98
laurent Avatar answered Sep 27 '22 17:09

laurent