Challenge - 5 Problems
API Versioning Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
How does Rails route to different API versions?
Given a Rails API with versioning using namespace, what will be the response when calling
GET /api/v2/products if only v1 namespace has a ProductsController?Ruby on Rails
Rails.application.routes.draw do
namespace :api do
namespace :v1 do
resources :products, only: [:index]
end
namespace :v2 do
# No products controller defined here
end
end
endAttempts:
2 left
💡 Hint
Think about how Rails matches routes and controllers inside namespaces.
✗ Incorrect
Rails routes are matched exactly. Since v2 namespace has no products controller or route, calling /api/v2/products results in 404 Not Found.
📝 Syntax
intermediate2:00remaining
Correct syntax for versioning with Accept header in Rails
Which option correctly configures Rails to use API versioning via the Accept header with version 'v1'?
Attempts:
2 left
💡 Hint
The Accept header is used to specify the media type and version.
✗ Incorrect
Option B correctly checks if the Accept header includes the custom media type with version 'v1'. Other options check wrong headers or content types.
❓ state_output
advanced2:00remaining
What is the response when version is missing in header?
In a Rails API that requires versioning via Accept header, what response code will the client get if the Accept header is missing?
Ruby on Rails
class Api::BaseController < ActionController::API before_action :check_version private def check_version unless request.headers['Accept']&.include?('application/vnd.myapp.v1+json') render json: { error: 'API version missing or unsupported' }, status: :not_acceptable end end end
Attempts:
2 left
💡 Hint
Check the status code used in the render call.
✗ Incorrect
The controller explicitly renders a 406 Not Acceptable status when the Accept header does not include the required version string.
🔧 Debug
advanced2:00remaining
Why does this versioned API controller not respond correctly?
Given this controller, why does the API always respond with 404 Not Found when requesting /api/v1/users?
Ruby on Rails
module Api
module V1
class UsersController < ApplicationController
def index
render json: { users: [] }
end
end
end
end
Rails.application.routes.draw do
namespace :api do
namespace :v1 do
get 'users', to: 'users#index'
end
end
endAttempts:
2 left
💡 Hint
Rails expects controller files to be in folders matching their module namespaces.
✗ Incorrect
If the controller file is not in app/controllers/api/v1/users_controller.rb, Rails will not find it and return 404.
🧠 Conceptual
expert3:00remaining
What is the main advantage of using URL path versioning over header versioning in Rails APIs?
Consider two common API versioning strategies in Rails: using URL path segments (e.g., /api/v1/) and using custom Accept headers. What is the main advantage of URL path versioning?
Attempts:
2 left
💡 Hint
Think about how URLs are used in browsers and caching systems.
✗ Incorrect
URL path versioning makes the API version explicit in the URL, which helps with testing, bookmarking, and caching. Header versioning requires clients to set headers correctly.