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?
REST uses various representations to represent a resource where Text, JSON, XML. The most popular representations of resources are XML and JSON.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With