Reputation: 1071
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 protected]" --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:
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
Upvotes: 1
Views: 1156
Reputation: 1158
A few things you could do:
constraints: { format: :json }
rescue_from Exception do |e|
render json: e.to_json, status: 500
end
Upvotes: 1