Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9196839
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T21:56:18+00:00 2026-06-17T21:56:18+00:00

In my rails app which is json only, I want to send a 406

  • 0

In my rails app which is json only, I want to send a 406 code whenever someone calls my rails app with accept header set to anything except application/json. I also want it to send a 415 when I get the content type set to anything except application / json

My controllers have respond_to :json put on them. I only render json in all actions. However how do I ensure that I return error code 406/415 for all calls for anything that is called for all other accept headers/content-type and with format set to anything except json.

Eg. If I my resource is books/1 I want to allow
books/1.json or books/1 with application/json in accept header and content type

Any ideas on how I can do these two actions?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T21:56:19+00:00Added an answer on June 17, 2026 at 9:56 pm

    Basically, you can limit your responses in two ways.

    First, there is respond_to for your controllers. This would automatically trigger a 406 Not Acceptable if a request for a format is made which is not defined.

    Example:

    class SomeController < ApplicationController
      respond_to :json
    
    
      def show
        @record = Record.find params[:id]
    
        respond_with @record
      end
    end
    

    The other way would be to add a before_filter to check for the format and react accordingly.

    Example:

    class ApplicationController < ActionController::Base
      before_filter :check_format
    
    
      def check_format
        render :nothing => true, :status => 406 unless params[:format] == 'json' || request.headers["Accept"] =~ /json/
      end
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So Im developing a Rails app- primarily serves API which I want to lock
I am working on a rails app in which I want to respond to
I have a rails 2.1 app with order which belongs to group. I want
i want to rebuild an app which is a typical rails 3.2 mvc app
I have an existing rails backend website which makes json calls to server. Now,I
I have a database-less rails app which communicates with an API. For authentication, I
I need to send a POST request from a sinatra app to a rails
I'm making the following cross-domain post, which hits my Rails app no problem. I
I am re-writing (yeah I know!) a Rails app which is largely API driven,
Sitting here with a simple rails 3 app in which I have a simple

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.