Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Link headers vs link elements for RESTful JSON

When building a RESTful / hypermedia API with JSON resources, it seems I have two options for specifying the hypermedia relationships between resources.

  1. Embed the links in the body of the JSON document. The problem here is that there isn't a standardized syntax for specifying hyperlinks, although I see a number of good efforts: (HAL, Collection+JSON, JSON-LD, JSON Schema to name a few).

  2. Use HTTP Link headers. This is a standardized, so this seems to have an advantage over embedded links. Clients just understand how to make sense of the standard header and voila, hypermedia goodness is achieved.

So, specifically in the context of handling JSON resources, which is the way to go and why?

like image 292
HolySamosa Avatar asked Mar 16 '12 18:03

HolySamosa


People also ask

What is the use of headers in REST API?

The REST headers and parameters contain a wealth of information that can help you track down issues when you encounter them. HTTP Headers are an important part of the API request and response as they represent the meta-data associated with the API request and response.

What is a link header?

The HTTP Link Header. The Link: header in HTTP allows the server to point an interested client to another resource containing metadata about the requested resource.

What is custom header in REST API?

Custom Headers are for troubleshooting, informational purposes, and specific server-side logic. For example, to send a GET request with a custom header name, you can use the "X-Real-IP" header, which defines the client's IP address. For a load balancer service, "client" is the last remote host.

What is HREF in REST API?

3.1. This is represented by the href attribute. Link relation type: The link relation type describes how the current context (source) is related to the target resource. This is represented by the rel attribute.


1 Answers

Go with a hypermedia JSON format. While Link Headers are standard, they're poorly adopted. They're really more valid for media formats that are not hypermedia. But since you have a choice and can choose a hypermedia format (unlike, say, PNG vs JPG), you should simply choose one and move forward.

All of the JSON standard are bubbling about until one or another becomes a "de facto" standard. The more they're used, the more "de facto" they get.

Seems to me that HAL is on a solid Standards track, and I would pick that.

But either way, go with a hypermedia format because you can.

like image 75
Will Hartung Avatar answered Sep 22 '22 21:09

Will Hartung