class ApiController < ApplicationController before_action :only_respect_accept_header private # By default, Rails will ignore the Accept header if it contains a wildcard # and assume the client wants HTML (or JS if using XMLHttpRequest). See # https://github.com/rails/rails/blob/a807a4f4f95798616a2a85856f77fdfc48da4832/actionpack/lib/action_dispatch/http/mime_negotiation.rb#L171-L173 # # If you don't expect your clients to be browsers, we want to override this # and only set the request formats from the Accept header, falling back to # the wildcard if it is not present (and therefore respecting our # priorities). # # Note we have to filter down the MIME types to ones Rails understands # otherwise it will raise an error when attempting to set formats for the # lookup context. def only_respect_accept_header request.set_header( "action_dispatch.request.formats", requested_mime_types.select { |type| type.symbol || type.ref == "*/*" } ) end def requested_mime_types Mime::Type.parse(request.get_header("HTTP_ACCEPT").to_s).presence || [Mime::ALL] end end