Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I use HATEOAS and Query Parameters for RESTful search?

I would like to design a RESTful search URI using query parameters. For example, this URI returns a list of all users:

GET /users

And the first 25 users with the last name "Harvey":

GET /users?surname=Harvey&maxResults=25

How can I use hypermedia to describe what query parameters are allowed by the "/users" resource? I noticed that the new Google Tasks API just documents all the query parameters in the reference guide. I will document the list, but I would like to do it with HATEOAS too.

Thank you in advance!

like image 342
Troy Harvey Avatar asked May 16 '11 19:05

Troy Harvey


People also ask

How do you pass multiple query parameters in restful web services?

@PUT @Path("{user}/{directory:. +}") public Response doshare(@PathParam("user")String name, @PathParam("directory")String dir, @QueryParam("name")String sharename, @QueryParam("type")String type){ mongoDAOImpl impl=new mongoDAOImpl(); Mongo mongo=impl. getConnection("127.0. 0.1","27017"); DB db=impl.

How do you pass a query parameter in REST?

Query parameters are passed after the URL string by appending a question mark followed by the parameter name , then equal to (“=”) sign and then the parameter value. Multiple parameters are separated by “&” symbol. The same parameters passed as URL parameters in the previous example are passed as Query parameters here.

What is HATEOAS in REST API?

It is a component of the REST application that distinguishes it from other network architecture. Using HATEOAS, a client interacts with a network application, whose application server provides information dynamically through Hypermedia.

What is a HATEOAS request?

A HATEOAS request allows you to not only send the data but also specify the related actions. When using HATEOAS architecture, a client will be able to access the API for a network application through a simple, static, RESTful URL call.

What is the use of a HATEOAS link?

HATEOAS allows the server to make URI changes as the API evolves without breaking the clients. Above API interaction is possible using HATEOAS only. Each REST framework provides its way of creating the HATEOAS links using framework capabilities.

What are the benefits of using HATEOAS?

Additionally, the clients no longer have to hardcode the URI structures for various resources. HATEOAS allows the server to make URI changes as the API evolves without breaking the clients. Above API interaction is possible using HATEOAS only. Each REST framework provides its way of creating the HATEOAS links using framework capabilities.


2 Answers

Using the syntax described in the current draft of the URI template spec you would do:

/users{?surname,maxresults} 
like image 161
Darrel Miller Avatar answered Sep 23 '22 20:09

Darrel Miller


The other option is to use an html form:

<form method="get" action="/users">    <label for="surname">Surname: </label>      <input type="text" name="surname"/>    <label for="maxresults">Max Results: </label>      <input type="text" name="maxresults" value="25"/> <!-- default is 25 -->    <input type="submit" name="submitbutton" value="submit"/> </form> 

A form like this fully documents the available options and any defaults, it creates the specified URL and can be annotated with any further documentation that you care to put there.

like image 29
stand Avatar answered Sep 21 '22 20:09

stand