Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RESTful API - Get last of an element [closed]

Tags:

rest

api

What's the best practice for getting the last added element (let's say we know that because of a created_at field on the resource).

Should it be a call to the get all with max results on 1 like:

GET ../rest/v1/article?page=0&size=1&order=created_at,desc

and will return an array of one element

or maybe an "special" call like:

GET ../rest/v1/article/last

and will return an element.

I am looking for a best practice if there's one pattern for this.

Thanks!

like image 846
Fernando Fradegrada Avatar asked Nov 29 '16 15:11

Fernando Fradegrada


People also ask

What is an endpoint in REST API?

Simply put, an endpoint is one end of a communication channel. When an API interacts with another system, the touchpoints of this communication are considered endpoints. For APIs, an endpoint can include a URL of a server or service.

What is request body in REST API?

A request body is data sent by the client to your API. A response body is the data your API sends to the client. Your API almost always has to send a response body. But clients don't necessarily need to send request bodies all the time.

What is REST endpoint URL?

A REST Service Endpoint is an endpoint which services a set of REST resources. The URI for REST Service Endpoints entities is: http://www.ibm.com/xmlns/prod/serviceregistry/profile/v8r0/RESTModel#RESTServiceEndpoint.

Which method can be used to quickly check whether a resource exists on the server or not?

One endpoint marked as GET. I this endpoint i can use GET method to fetch data and HEAD to check if resource is available. Two endpoints.


1 Answers

I'm not a RESTful expert, but in my opinion the first solution seems the best.

The second is more practical, but routes are often associated with resources, the addition of a "last", especially preceded by a "/" seems strange to me.

In addition, API users usually use the sorting parameters, and what about users which need 10 last elements ?

If you add something after ../rest/v1/article, it must be an ID for one particular element, a sub-resource, or for actions that are outside the CRUD like ../rest/v1/article/:id/subscription.

like image 173
fnev.eu Avatar answered Sep 23 '22 08:09

fnev.eu