6

suppose the DogManagementPro program is an application written in client/server architecture, where the customers who buys it is supposed to run the server on his own PC, and access it either locally or remotely.

suppose I want to support a "list all dogs" operations in the DogManagementPro REST API.

so a GET to http://localhost/DogManagerPro/api/dogs should fetch the following response now:

<dogs>
  <dog>http://localhost/DogManagerPro/api/dogs/ralf</dog>
  <dog>http://localhost/DogManagerPro/api/dogs/sparky</dog>
</dogs>

where I want to access it remotely on my local LAN, [the local IP of my machine is 192.168.0.33] what should a a GET to http://192.168.0.33:1234/DogManagerPro/api/dogs fetch?

should it be:

<dogs>
  <dog>http://localhost/DogManagerPro/api/dogs/ralf</dog>
  <dog>http://localhost/DogManagerPro/api/dogs/sparky</dog>
</dogs>

or perhaps:

<dogs>
  <dog>http://192.168.0.33/DogManagerPro/api/dogs/ralf</dog>
  <dog>http://192.168.0.33/DogManagerPro/api/dogs/sparky</dog>
</dogs>

?

some people argue that I should subside the problem altogether by returning just a path element like so:

<dogs>
  <dog>/DogManagerPro/api/dogs/ralf</dog>
  <dog>/DogManagerPro/api/dogs/sparky</dog>
</dogs>

what is the best way?

Aviad Rozenhek
  • 2,259
  • 3
  • 21
  • 42
  • There is also the option of using a domain. You would then have `http://dogmanagerpro/api/dogs/sparky` Where `dogmanagerpro` would be set up to point to the specific machine:port where the server is listening. This option is the most flexible with regard to changing physical addresses, but it requires your clients to run their own domain server (DNS server) that can map DogManagerPro to an absolute address. – Marjan Venema Jul 13 '12 at 06:36

2 Answers2

7

I've personally always used non-absolute urls. It solves a few other problems as well, such as reverse / caching proxies.

It's a bit more complicated for the client though, and if they want to store the document as-is, it may imply they also now need to store the base url, or expand the inner urls.

If you do choose to go for the full-url route, I would not recommend using HTTP_HOST, but setup multiple vhosts, and environment variable and use that. This solves the issue if you later on need proxies in front of your origin server.

Evert
  • 93,428
  • 18
  • 118
  • 189
  • relative URLs seem easiest from server side and many people recommended that – Aviad Rozenhek Jul 15 '12 at 14:19
  • @Aviad you should say "many server-side people recommended that". :) But that is just one side of the story. You need to consider client-side too. See [this answer](http://stackoverflow.com/questions/2239405/hateoas-absolute-or-relative-urls/2240380#2240380) and [this is my understanding too](http://stackoverflow.com/questions/2239405/hateoas-absolute-or-relative-urls/18505331#18505331) – RayLuo Aug 29 '13 at 08:05
  • @iceberg I mean relative URI and absolute path – Aviad Rozenhek Aug 29 '13 at 20:20
0

I would say absolute URLs created based on the Host header that the client sent

<dogs>
  <dog>http://192.168.0.33:1234/DogManagerPro/api/dogs/ralf</dog>
  <dog>http://192.168.0.33:1234/DogManagerPro/api/dogs/sparky</dog>
</dogs>

The returned URIs should be something the client is able to resolve.

Ian Roberts
  • 120,891
  • 16
  • 170
  • 183