Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where in a HATEOAS architecture do you specify the HTTP verbs?

Tags:

rest

hateoas

I was reading an article on HATEOAS and while I understand the idea of providing the URLs for further actions in the response, I don't see where you specify what HTTP verbs should usedto interact with those URLs.

For example, from What is HATEOAS and why is it important for my REST API?, how from this response

GET /account/12345 HTTP/1.1  HTTP/1.1 200 OK <?xml version="1.0"?> <account>     <account_number>12345</account_number>     <balance currency="usd">100.00</balance>     <link rel="deposit" href="/account/12345/deposit" />     <link rel="withdraw" href="/account/12345/withdraw" />     <link rel="transfer" href="/account/12345/transfer" />     <link rel="close" href="/account/12345/close" /> </account 

do you know if I should issue an HTTP PUT or POST to /account/12345/close?

like image 522
Sled Avatar asked Nov 13 '13 16:11

Sled


People also ask

Which is the HTTP verb used in RESTful services?

The primary or most-commonly-used HTTP verbs (or methods, as they are properly called) are POST, GET, PUT, PATCH, and DELETE. These correspond to create, read, update, and delete (or CRUD) operations, respectively.

Why do we use HTTP verbs in Web API?

The above is considered verb-based routing. The URLs above only contain the controller name and an optional id. So the Web API uses the HTTP verb of the request to determine the action method to execute in your ApiController subclass.

Should we use HTTP verbs in Uri?

HTTP verbs are preferred if possible, as they are part of the HTTP protocol and as such a standard. It also allows you to use existing security and caching layers on a standard web server, without having to write any bespoke middleware.


1 Answers

Don't puts verbs in your URIs (eg /account/12345/transfer). URIs represent resources, not actions.

The verbs to use are defined by the HTTP protocol (eg GET, POST, PUT, OPTIONS, DELETE etc). REST is a architecture design with a set of constraints, and HTTP is a protocol that adheres to these constraints. HTTP defines a limited set of verbs to transfer the state of a resource from client to server and vice versa. By definition you are constrained to these verbs only.

The client should decide what HTTP verb to use based on what it is trying to do. The server doesn't need to tell it what verbs there are, it already knows based on the HTTP protocol.

If the client needs to know what verbs it can use on a resource it can query the resource using the OPTIONS verb and look at Allow header in the response (assuming the server returns this information, which it should if it is being helpful). Some resources might only accept GET, while others may accept others such as POST and PUT.

Have a look at the HTTP specification to see what verb to use in what context.

To give an example from your original post. Say you have an account resource with a URI at

/accounts/12345 

and you want to close the account. Remember REST is state transfer. The client is closing the account so it has the account in a state of closed on its end. It then transfers that state to the server so that the client and server both are in line with each other. So you PUT the clients state (which is the resource in a closed state) onto the server

PUT /accounts/12345 

The body of the request should contain a representation of the resource in a closed state. Assuming you are using XML to represent the account resource it would be something like this

PUT /accounts/12345  <?xml version="1.0"?> <account>     <account_number>12345</account_number>     <balance currency="usd">100.00</balance>     <state>closed</state> </account> 

The resource on the server now mirrors the resource on the client. Both are in a closed state. If you don't want to transfer the whole resource every time you make a change to one of its attributes you could split them out into a resource hierarchy. Make the status of the account its own resource and PUT to that to change it

PUT /accounts/12345/status  <?xml version="1.0"?> <state>closed</state> 
like image 171
Cormac Mulhall Avatar answered Nov 24 '22 02:11

Cormac Mulhall