vendredi 20 septembre 2019

How to Configure CORS Accept Headers in Rails

i am trying to configure CORS accept headers in rails that when i did as below it doesnt seem to allow me to do so as it will threw back this 404 with the OPTIONS request

application controller

class ApplicationController < ActionController::Base
  before_action :set_paper_trail_whodunnit, :cors_set_access_control_headers
  skip_before_filter :verify_authenticity_token

  def verify_api_key
    return if request.headers["HTTP_API_KEY"] == Rails.configuration.x.api_key
    render json: { status: :unauthorized }, status: :unauthorized
  end
def cors_preflight_check
  if request.method == 'OPTIONS'
    cors_set_access_control_headers
    render text: '', content_type: 'text/plain'
  end
end

protected

def cors_set_access_control_headers
  response.headers['Access-Control-Allow-Origin'] = '*'
  response.headers['Access-Control-Allow-Methods'] = 'POST, GET, PUT, PATCH, DELETE, OPTIONS'
  response.headers['Access-Control-Allow-Headers'] = 'Origin, Content-Type, Accept, Authorization, Token, Auth-Token, Email, X-User-Token, X-User-Email'
  response.headers['Access-Control-Max-Age'] = '1728000'
end
end


application.rb


    config.action_dispatch.default_headers = {
    'Access-Control-Allow-Origin' => '*',
    'Access-Control-Request-Method' => %w{GET POST OPTIONS}.join(","),
    'Access-Control-Allow-Methods' => %w{GET POST OPTIONS}.join(",")
}


can anyone help?

Aucun commentaire:

Enregistrer un commentaire