Skip to content

Instantly share code, notes, and snippets.

@philsturgeon
Created October 20, 2016 14:28
Show Gist options
  • Save philsturgeon/5326d0c6fb741650df51b7e3d8748338 to your computer and use it in GitHub Desktop.
Save philsturgeon/5326d0c6fb741650df51b7e3d8748338 to your computer and use it in GitHub Desktop.
RESTful Liking

Option 1

Modifying a global resource can feel a bit weird, but essentially as an authorized user, you are looking at your customized representation of the resource, and not just a direct 1:1 of whats in the database.

  • Like: PATCH /bars/123 with field liked: true
  • Unlike: PATCH /bars/123 with field liked: false

This can feel a bit weird if you consider it as corrupting the global value, but headers often change the response and that's probably fine. It can be weird to show different attributes depending on the user (I avoid this) but you can probably jam it in meta as the JSON-API allows this already? It's relevant data, just not necessairily part of the resource.

Option 2

Likes are a subcollection, which you can add and delete too.

  • Like: POST /bars/123/likes
  • Unlike: DELETE /bars/123/likes/XYZ

Option 3

Likes are a JSON-API relationship with their own relationship URL on the resource.

  • Like: POST /bars/1/relationships/likes
  • Unlike: DELETE /bars/1/relationships/likes/XYZ

This is simple and very like just doing usual resources, but a resource might have more information than the relationship collection, which is just the basics:

{
  "data": { "type": "people", "id": "myidnumber" }
}

Feels a bit weird telling the API you're a person though ey?

Option 4

Make your very own little land of likes:

  • Like: POST /me/relationships/likes
  • Unlike: DELETE /me/relationships/likes/XYZ

You then get to send

{
  "data": { "type": "bar", "id": "123" }
}

Seems a bit cleaner, and leaves the usual /bars/123/likes to be a collection of full fat resources.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment