How do I declare Content-Range in the Access-Control-Expose-Headers header using rack-cors

1.3k Views Asked by At

In order to access my api from frontend it is asking I declare Content-Range in the Access-Control-Expose-Headers header. I can't figure out exactly how to write it.

Rails.application.config.middleware.insert_before 0, Rack::Cors do
  allow do
    origins '*'

    resource '*',
      headers: ["Access-Control-Expose-Headers", "Content-Range: 0-24/319"], 
      methods: [:get, :post, :put, :patch, :delete, :options, :head]
  end
end

or

Rails.application.config.middleware.insert_before 0, Rack::Cors do
  allow do
    origins '*'
    resource '*',
      headers: :any, 
      expose: ["Content-Range: orders 0-24/319"],
      methods: [:get, :post, :put, :patch, :delete, :options, :head]
  end
end

Any idea what I've written incorrectly?

1

There are 1 best solutions below

3
zhisme On

according to test case in original repo there is no possibility to pass key: value pair inside routes definition that would be accepted by rack-cors

Rails.application.config.middleware.insert_before 0, Rack::Cors do
  allow do
    origins '*'
    resource '*',
      headers: :any, 
      expose: ["Content-Range"],
      methods: [:get, :post, :put, :patch, :delete, :options, :head]
  end
end

and then for example, you can write default value in BaseController and then inherit every controller from it

class BaseController < ApplicationController
  after_action :apply_content_range_header

  protected
  def apply_content_range_header
    response.headers['Content-Range'] = 'orders 0-24/319'
  end
end

and then in your controller just call it

class ProductsController < BaseController
  def index; end # your products#index will have Content-Range header
end