简体   繁体   中英

Rails - remove HTML from api JSON responses

Using rails (4.0.2)
Using rails-api (0.1.0)

When rails-api has an error like a 500 it throws a bunch of html in the response, despite all other requests format the output to json.

Any way to remove the html from the response? A simple 500 response, and the stack trace in the logs would be great.

UPDATE: I have removed these files from the public folder:

404.html
422.html
500.html

But still getting html in the response.

I believe it is getting caught up in: https://github.com/rails/rails/blob/043c8707bf0a87beb3906643ddfd1b60f52c1a98/actionpack/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb

You can tell your routes to return JSON by default

namespace :api, defaults: {format: :json} do
  namespace :v1 do
    namespace :items do
      get :all
    end
  end
end

Then in your controller,

render json: { json_structure: true }, status: :internal_server_error

If I were you, I'd follow something like this guide to make sure you've done all the necessary steps for making a standard rails app an API.

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