Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generating Hypermedia links in a Web API

Tags:

I'm curious to know how others have dealt with the issue of generating hypermedia links for their web APIs? Specifically, I'm using ASP.NET Web API, and am torn between having operations return hypermedia-related types, or returning the resource itself, and having the hypermedia stuff happen later in the pipeline. That is, do people tend to do things like:

public Resource<Order> GetOrder(int id) {    return new Resource<Order>() {       Content = new Order(),       Links = new LinkCollection<Order>() { new AddOrderLink(), new UpdateOrderLink()}   } 

Or something more like

public Order GetOrder(int id) { return new Order(); } 

And then add hypermedia links inside an HttpOperationHandler or custom formatter or something?

If the approach is more like #2, how do you know what links to generate? Just have some standard set of links that get generated for all Order objects? Attributes decorating various operations in OrdersController?

like image 834
Jordan0Day Avatar asked Sep 05 '12 21:09

Jordan0Day


People also ask

CAN REST API support hypermedia links?

REST architectural style lets us use the hypermedia links in the API response contents. It allows the client to dynamically navigate to the appropriate resources by traversing the hypermedia links.

What is hypermedia in API?

A hypermedia API is an API that returns hypermedia, typically HTML over HTTP. This style of API is distinguished from data APIs that do not return a hypermedia. The most familiar form of this latter style of API today is the ubiquitous JSON API.

What are HATEOAS links?

1. What is HATEOAS? HATEOAS is a constraint on REST that says that a client of a REST application need only know a single fixed URL to access it. Any and all resources should be discoverable dynamically from that URL through hyperlinks included in the representations of returned resources.


2 Answers

I prefer option two (adding the hypermedia links later in the pipeline) and blogged about doing this yesterday.

The solution was to "enrich" my resources with hypermedia links before they are returned to the client using a message handler.

like image 171
Ben Foster Avatar answered Oct 12 '22 13:10

Ben Foster


You can use the Hyprlinkr from github

I'm planning to use it in my next project as it seens to be nice and easy to do it and you can get it via nuget package.

like image 42
Diego Garcia Avatar answered Oct 12 '22 11:10

Diego Garcia