简体   繁体   中英

Rails insist in returning html on responses

I've been trying to configure a rails restful API service.

I've set the project up with Rails-API , and added token authentication with devise using this . My goal is that all responses are to be in JSON format, and that is what I thought that rails-api was going to accomplish

After running the devise rake generator, I tried to interact with the sign up endpoint, which I do with this curl request to the server:

curl --form "email=foo@email.com" --form "password=abc123" --form "password_confirmation=abc123" -H "Accept: application/json" -H "Content-type: application/json" -X POST --dump-header headers http://api.local.dev:3000/

I get this reply:

<html lang="en">
<head>
  <meta charset="utf-8" />
  <title>Action Controller: Exception caught</title>
  <style>
    body {
      background-color: #FAFAFA;
      color: #333;
      margin: 0px;
    }
.....

the page, when rendered, is just an exception trace:

呈现的html响应

On the server output, these lines show that actionpack middleware is still dumping his foul, unrequested trace html:

 Rendered /home/diffeo/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/actionpack-4.2.1/lib/action_dispatch/middleware/templates/rescues/_source.erb (2.6ms)
  Rendered /home/diffeo/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/actionpack-4.2.1/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb (1.7ms)
  Rendered /home/diffeo/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/actionpack-4.2.1/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb (0.9ms)
  Rendered /home/diffeo/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/actionpack-4.2.1/lib/action_dispatch/middleware/templates/rescues/diagnostics.html.erb within rescues/layout (20.0ms)

What I want to understand is how to FULLY disable html responses for everything

For reference, here is my Gemfile

source 'https://rubygems.org'


gem 'rails', '4.2.1'

gem 'rails-api'

gem 'pg'
gem 'activerecord-postgis-adapter'
gem 'rgeo'
gem 'devise'
gem 'devise_token_auth' # Token based authentication for Rails JSON APIs
gem 'omniauth' # required for devise_token_auth

# To use ActiveModel has_secure_password
# gem 'bcrypt', '~> 3.1.7'

# To use Jbuilder templates for JSON
# gem 'jbuilder'

# Use unicorn as the app server
# gem 'unicorn'

# Deploy with Capistrano
# gem 'capistrano', :group => :development

# To use debugger
# gem 'ruby-debug19', :require => 'ruby-debug'

group :development, :test do
    gem 'pry-byebug', '=1.3.3'
    gem 'pry-stack_explorer'
    gem 'pry-rails'
    gem 'pry-remote'

  # Access an IRB console on exception pages or by using <%= console %> in views
  gem 'web-console', '~> 2.0'

  # Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
  gem 'spring'

  gem "rspec-rails", "~> 3.3"
end

group :test do

  #gem "shoulda-matchers"
  gem "factory_girl_rails"
  gem 'ffaker'
end

Here is my routes.rb

Rails.application.routes.draw do
  namespace :api, constraints: { format: 'json' } do
    scope :v1 do
      mount_devise_token_auth_for "User", at: 'auth'
    end
  end
end

my user model:

class User < ActiveRecord::Base
  # Include default devise modules.
  devise :database_authenticatable, :registerable,
          :recoverable, :rememberable, :trackable, :validatable,
          :confirmable, :omniauthable
  include DeviseTokenAuth::Concerns::User
end

A few things you could do:

  1. Add a constraint to your routes so they only serve JSON (which you already did) IE:

constraints: { format: :json }

  1. Add a custom exception handler in the application controller: rescue_from Exception do |e| render json: e.to_json, status: 500 end rescue_from Exception do |e| render json: e.to_json, status: 500 end

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM