MrkK
MrkK

Reputation: 903

adding HTTP method to Spring HATEOAS links

I am wondering if it is possible to add HTTP method to the links created with Spring HATEOAS. I would like the link to look something like:

{
    "href":http://localhost:8080/admin/users",
    "rel": "add",
    "method": "POST"
}

{
    "href":http://localhost:8080/admin/users/john",
    "rel": "remove",
    "method": "DELETE"
}

I couldn't find anything which would allow me to add "method" to the link.

Upvotes: 7

Views: 9814

Answers (6)

s1mm0t
s1mm0t

Reputation: 6095

Spring HATEOAS appears to support affordances now which allows you to build hypermedia that affords operations using HAL-FORMS. This would have allowed the OP to achieve something similar to what was requested albeit with a different (and more complex [flexible?]) schema.

https://github.com/spring-projects/spring-hateoas-examples/tree/master/affordances

Upvotes: 1

Michael De Courci
Michael De Courci

Reputation: 19

I have designed REST APIs based on the Richardson model; http://martinfowler.com/articles/richardsonMaturityModel.html

All endpoints returned the "allowed" links with an HTTP method. This design approach allowed the consumer to know the HTTP method to use, rather than calculate what method to use. This was useful since some operations used POST or PUT. POST is used to create a resource or change the state of a resource. PUT was used to update an existing resource and delete to remove a resource.

Including the HTTP method in a link is a powerful mechanism that enables the consumer of the API to know how to call the API.

Upvotes: 0

mikehwang
mikehwang

Reputation: 538

A link relation alone should be able to instruct clients how to use the link so I think having the method parameter in the link would be redundant. The definition of the link relation should have the details of what are the acceptable HTTP methods.

Also, I think the method parameter approach makes your system more confusing and more error-prone.

Let's say you have a link relation do-something which was initially designed to only allow for POSTs. Then the creator of do-something wishes to change that characteristic and switch to PUTs only. Having the method parameter in the links makes you become susceptible to an inconsistency between the "source of truth" (the link relation definition) and the servers that provide the links.

Upvotes: -1

Dick Chesterwood
Dick Chesterwood

Reputation: 2659

Consider implementing a HTTP OPTIONS at the URIs you've specified. This would respond with the valid options for that resource. It's not often done, but for me OPTIONS is a perfect way to help clients understand what is permitted.

Excellent blog post here: http://zacstewart.com/2012/04/14/http-options-method.html

Upvotes: 1

Waldemar Schneider
Waldemar Schneider

Reputation: 174

You should use the relation "edit".

In the section (11.1) from the Atom Pub RFC (https://www.rfc-editor.org/rfc/rfc5023) which defines that you can send PUT/DELETE/GET requests to this URI of the edit relation.

Upvotes: 1

That wouldn't make sense. The href specifies the address of some resource, and the rel tells how it's related to the current resource. The HTTP method indicates what the client wants to do to it, which isn't a part of the relationship between the two.

In your example, the "remove" relation doesn't make sense: HTTP specifies the DELETE verb, and the semantics of

DELETE http://localhost:8080/admin/users/john

are already known. Similarly, POST creates a new resource, so specifying /admin/users is sufficient for a client to list the users (with GET) or add a new user (with POST).

Upvotes: 2

Related Questions