REST versioning – URL vs. header

Both mechanisms are valid. You need to know your consumer to know which path to follow. In general, working with enterprises and academically-minded folks tends to point developers towards Resource Header versioning. However, if your clients are smaller businesses, then URL versioning approach is more widely used.

The Pros and Cons (I’m sure there are more, and some of the Cons have work-arounds not mentioned here)

  1. It’s more explorable. For most requests you can just use a browser, whereas, the Resource Header implementation requires a more programatic approach to testing. However, because not all HTTP requests are explorable, for example, POST requests, you should use a Rest Client plugin like Postman or Paw. URI Pro/Header Con

  2. With a URI-versioned API, resource identification and the resource’s representation is munged together. This violates the basic principles of REST; one resource should be identified by one and only one endpoint. In this regard, the Resource Header versioning choice is more academically idealistic. Header Pro/URI Con.

  3. A URI-versioned API is less error prone and more familiar to the client developers. Versioning by URL allows the developer to figure out the version of a service at a glance. f the client developer forgets to include a resource version in the header, you have to decide if they should be directed to the latest version (which can cause errors when incrementing the version) or a 301 (Moved Permanatly) error. Either way there is more confusion for your more novice client developers. URI Pro/Header Con
  4. URI versioning lends itself to hosing multiple versions in the same application. In this case you do not have to further development your framework.
    Note: If you do this your directory structure will most likely contain a substantial amount of duplicate code in the v2 directory. Also, deploying updates requires a system restart – Thus this technique should be avoided if possible. URI Pro/Header Con.
  5. It is easier to add versioning to the HTTP Headers for an existing project that didn’t already have versioning in mind from it’s inception. Header Pro/URI Con.
  6. According to the RMM Level 3 REST Principle: Hypermedia Controls, you should use the HTTP Accept and Content-Type headers to handle versioning of data as well as describing data. Header Pro/URI Con.

Here are some helpful links if you want to do some further reading:

  • Martin Fowler’s description of the Richardson Maturity Model
  • API Versioning – Pivotal Labs
  • HATEAOS
  • Informit.com’s Article on Versioning REST Services

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)