-
Notifications
You must be signed in to change notification settings - Fork 60
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Should _links only contain links with GET method ? #24
Comments
You have the HTTP method OPTIONS for query about available actions. |
Sometimes it is not feasible to do yet another query to get options, you'd want to include available actions in the entity:
or
|
You can just use a normal link and document the relation to specify what
|
Hi, I found this thread because I was actually thinking in the same direction as @redben was. To explain myself, please consider having this link as mentioned in examples: {
"_links": {
"next": { "href": "/page=2" }
}
} I suppose the server is going to omit the "next" link once there is no next page that can be followed with the link. Using this analogy I can think of resources that are available using GET method only (as well as resources available using POST only -- for example as mentioned in http://stackoverflow.com/questions/16877968/call-a-server-side-method-on-a-resource-in-a-restful-way#answer-16878494). Unfortunately, I won't be able to tell this from hal response itself. As @Maks3w wrote, we are able to find out what methods are supported by the resource using OPTIONS method, however you'd need to make OPTIONS request to server for every given link to retrieve this information. In HAL I love the mechanism that using links the API tells me that there still is other "next" resource available. However, I have to say I miss similar mechanism for the linked resources to be able to list methods that are available. Without this, Hal browser is going to offer invalid actions (methods) on resources (and is going to get 404's or 400's from server needlessly). In my opinion, the information "what you can or cannot do with the linked resource" could be encapsulated in _link itself. Otherwise, if I wanted to be precise and not do anything bad with the api, I'd have to rely on making OPTIONS request on every provided _link. In REST api there is pretty much clear what each HTTP method is going to do to the resource. Using dog's barking example from the StackOverflow link above, I think that simple listing of available methods would be sufficient. So at the end I'd love to retrieve something like this from server: {
"_links": {
"barks": { "href": "/v1/dogs/1/barks", "methods": ["POST"] }
}
} Of course I am aware that my servers can return whatever I want but I see this optional "methods" property as something that is missing in HAL specification itself :-). I hope I made my motives clear. If I'm misreading something, please do correct me where my mindset is wrong and why all of this is bad idea. Thank you! |
The "HAL way" of doing this is to use the link rel documentation to convey the available methods in a human readable form. ie. your barks example would look like this (notice the "barks" link rel is now a URL) {
"_links": {
"http://docs.example.com/barks": { "href": "/v1/dogs/1/barks" }
}
} and if a developer fetches the URL |
What if the available methods are variable, for example POST would be available for an open discussion (imagine this is the type of resource) but not for a closed one. I think @vchrm 's suggestion, the methods field, is great. |
@redben this breaks down if you have variability in behaviour beyond the method (eg. admins can still POST administrative stuff to a closed discussion). In my experience, it's easier to convey these types of conditional transition through rules attached to the potential state of a resource ie. documentation for a link that says "when the discussion is 'open' (has no |
@mikekelly I see what you mean. Conveying these rules in the state of the resource or making it explicit tends to be more of an developer (consumer) experience (DX, gush someone already coined this buzz term). The options are :
|
@redben I think this already is possible with use of _embedded. From the spec: "Embedded Resources MAY be a full, partial, or inconsistent version of the representation served from the target URI." By taking advantage of the "inconsistency" part of that statement we could end up with something like {
"_links": {
"self": {
"href": "https://blog/posts/example-1"
},
"comments": {
"href": "https://blog/posts/example-1/comments",
},
},
"_embedded": {
"comments": {
"_meta" : {
"examples": { "https://blog/comments?post=example-1" },
"method": "POST"
}
}
}
} _meta is just a container to prevent conflicts with other attributes you want to embed. Alternatively you you can do it something like this to be able to provide examples for each method. {
"_links": {
"self": {
"href": "https://blog/posts/example-1"
},
"comments": {
"href": "https://blog/posts/example-1/comments",
},
},
"_embedded": {
"comments": {
"_meta" : {
"_methods" : {
"POST": {
"examples": { "https://blog/comments?post=example-1" }
}
}
}
}
}
} This approach is what we are considering right now. Feedback are more than welcome! |
Not sure, if this discussion is still open, but I have some experience of implementing APIs based on latest published draft of HAL spec at this moment and I think this issue is still valid. Here's my story: Since there's no "method" specification in resource link currently and we'd like to keep close to the proposed standard (we prefer HAL to other existing specs for a number of reasons), we apply almost all workarounds possible:
We did not yet consider That said, I'd vote for optional |
Would love to see a standard solution to this in HAL for the same reasons mentioned by @ivan-gammel, @vchrm, @redben... I would love to see in one response in a standard way (so libs can be build on top of this), if my current person is allowed to delete something or not for example. |
I just found out about HAL-FORMS. Never heard of this. Is this popular to use in the HAL community? (Would require |
Say an api exposes blog posts and with each blog post, one would want to include a link to post comments, should that links be in
_links
? or should there be some other property like_actions
?The text was updated successfully, but these errors were encountered: