Today, while checking my APM (Application Performance Monitoring) tools, I found some strange errors in the reports. After looking closer, I realized these errors were coming from people trying to access my webhook URLs by adding format extensions like /blog.xml
or /blog.json
. Even though these endpoints don’t exist in my app, these requests were creating unnecessary error alerts.
In this post, I’ll show you how to disable these format extensions in Rails to avoid similar issues.
By default, Rails allows URLs to end with format extensions (like .xml, .json, .csv). This means when someone tries to access:
GET /webhooks.xml
GET /webhooks.json
Rails will try to respond to these requests. If you haven’t set up your app to handle these formats, it returns a 404 error, which then shows up in your error monitoring tools.
There are three ways to solve this problem. Choose the one that fits your needs best:
This is the simplest solution. In your config/routes.rb
file, add:
Rails.application.routes.draw do
default_format :html
# Your routes here
resources :webhooks, format: false
end
If you want to keep formats for some routes (like an API) but disable them for others:
Rails.application.routes.draw do
# API routes that can still use formats
namespace :api do
resources :webhooks
end
# Regular routes without formats
scope format: false do
resources :webhooks
end
end
For more control over which routes accept formats:
Rails.application.routes.draw do
constraints format: false do
get '/blog', to: 'blog#index'
resources :posts
end
# Routes outside this block can still use formats
end
source constraint: https://guides.rubyonrails.org/routing.html#specifying-constraints
rails routes
to see how your changes affected your routesAfter I made these changes: