2

Let's say we have a RESTful API method:

POST /people
{
    "name" : "John",
    "_links" : {
        "address" : {
            "href" : "/addresses/2"
        }
    }
}

You can see that address has a link to another resource.

To resolve the address_id of that resource, should the server:

  1. Break up the URL and identify the "id" part of the route

  2. Even make a curl request to itself, in order to get the address_id of that linked resource?

eoinoc
  • 3,155
  • 3
  • 24
  • 39

1 Answers1

1

I feel, as you probably do, that #2 is the way it should be done, and #1 is just a hack based on internal knowledge. What if the client sent a href that was an absolute URI, http://yoursite.com/addresses/2 would you still want to use #1, and try to detect the ID?
Lets presume your site sends address resource representations with a defined and documented MIME type. What if the client sent a href value pointing to a third-party URI, which also returned responses of that format, and presuming you'd want to support that. You'd have to implement #2 anyway. In that instance there's little reason not to use it for your own (performance being the main one).

To be honest, what I would probably do is go with #1 until the need for #2 arose.

Nicholas Shanks
  • 10,623
  • 4
  • 56
  • 80
  • Sounds reasonable. It would feel a bit much for an app to have to speak to "itself" over HTTP. Any ideas if apps that are built on a foundational API (such as Twitter.com) call themselves over HTTP? – eoinoc May 29 '13 at 10:32
  • 1
    Many client-server applications (I'm thinking of multiplayer games mostly) speak to localhost over TCP/IP. Modern OSes short circuit the networking stack when this occurs, and you get something as good as an IPC socket. Having a lightweight reverse proxy such as Nginx or Lighttpd in front of Apache will mean much of the HTTP costs are negated, but you still have to pay the IPC penalty, versus doing everything in the one process. The benefits are that you only have one code path to handle "data requests". – Nicholas Shanks May 29 '13 at 10:47