0

I have a Rails 6 app, where I am upgrading some functionalities and upgrading the API version to v2. There are older clients out there that are using the v1 API versions. For all these, I have to change the incoming url in v1 to v2.

Example, a request comes in at /api/v1/users, I want it to change to /api/v2/users.

I've tried few things like using redirect in routes.rb file, like the ones suggested in API Versioning for Rails Routes. However, I could not get it too work.

My routes.rb file contains something like this:

Rails.application.routes.draw do
 namespace 'ads' do
  get '/fingerprint' => 'fingerprint#index'
 end

 namespace 'api' do
   scope '/v2' do
     ...
   end
 end

end

I'd like to add something like

namespace 'api' do

   scope '/v1' do
     # redirect to v2 version of the url 
   end

   scope '/v2' do
     ...
   end
 end

I'd like to fix it within the Rails app, but I guess fixing at Apache is also an option. I'm open to any sorts of solutions. Thanks.

anz
  • 987
  • 7
  • 21
  • Can you share an example route for the v1 and v2 scopes? – Cameron Feb 24 '22 at 01:10
  • You can probally use a catch-all such as 'get '*', { |path_params, req| ... }`. Not sure its actually a good idea to automatically forward clients as it breaks the whole idea of a versioned API which is that the responses should stay consistent within a version until its discontued. A redirect will also break any client thats not configured to follow redirects anyways. https://guides.rubyonrails.org/routing.html#redirection – max Feb 24 '22 at 02:26
  • Using something like ['/v1','/v2'].each do |current_scope| scope current_scope do ... seems to work. I don't really see any downsides to it. – anz Feb 24 '22 at 23:05

0 Answers0