Skip to content

Instantly share code, notes, and snippets.

@saml
Last active August 29, 2015 14:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save saml/750f80796a6ac508de29 to your computer and use it in GitHub Desktop.
Save saml/750f80796a6ac508de29 to your computer and use it in GitHub Desktop.

Example article API.

An article can have one main image. And many secondary images.

GET /articles/1

{
  "_links": {
    "self": {"href": "/articles/1"},
    "image": {
      "href": "/images/a.jpg",
      "width": 1000,
      "height": 2000,
      "credit": "Getty Images"
    },
    "image:secondary": [{
      "href": "/images/b.jpg",
      "width": 500,
      "height": 500,
      "credit": "Getty Images"
    }, {
      "href": "/images/c.jpg",
      "width": 1000,
      "height": 700,
      "credit": "Creative Commons"
    }]
  },
  "title": "abc",
  "html": "<p>hello <img src=\"/images/a.jpg\"></p><p><img src=\"/images/b.jpg\"><img src=\"/images/c.jpg\"></p>"
}

To

/articles/1 has links to self and image.

Representation is representation (of a resource, which is abstract). _links and _embedded are auxiliary. Not really part of /articles/1 in a way.

You can PUT like the following in an attempt to modify linked image resource:

GET /articles/1

{
  "_links": {
    "self": {"href": "/articles/1"},
    "image": {"href": "/images/a.jpg"}
  },
  "_embedded": {
    "image": {
      "_links": {
        "self": {"href": "/images/a.jpg"}
      },
      "credit": "Flickr"
    }
  },
  "title": "abc",
  "html": "<p>hello</p>"
}

But server ignores _links and _embedded. So, above PUT request is same as:

PUT /articles/1

{
  "title": "abc",
  "html": "<p>hello</p>"
}

If you actually want to modify the image, you should follow the link (doc._embedded.image._links.self.href).

GET /images/a.jpg

{
  "_links": {
    "self": {"href": "/images/a.jpg"}
  },
  "credit": "Getty Images"
}
PUT /images/a.jpg

{
  "credit": "Flickr"
}

# responds with

{
  "_links": {
    "self": {"href": "/images/a.jpg"}
  },
  "credit": "Flickr"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment