Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

REST versioning - URL vs. header

Tags:

I am planning to write a RESTful API and I am clueless how to handle versioning. I have read many discussions and blog articles, which suggest to use the accept header for versioning.

But then I found following website listening popular REST APIs and their versioning method and most of them using the URL for versioning. Why?

Why are most people saying: "Don't use the URL, but use the accept header", but popular APIs using URL?

like image 878
Georg Leber Avatar asked Sep 19 '13 21:09

Georg Leber


People also ask

What is a disadvantage of using URL path segment versioning in an API?

CONS. It disrupts the RESTful compliance: URIs should represent resources and not versions (one URI = one resource/resource version). Just like the previous method you should try to avoid this method if you are also versioning resources.

What is the most common method of versioning a REST API?

URI Path. The most common way to version an API is in the URI path. This method employs URI routing to direct requests to a specific version of the API.

Should an API be versioned?

APIs only need to be up-versioned when a breaking change is made. Breaking changes include: a change in the format of the response data for one or more calls. a change in the request or response type (i.e. changing an integer to a float)


1 Answers

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
like image 158
cosbor11 Avatar answered Nov 08 '22 23:11

cosbor11