Help us understand the problem. What is going on with this article?

RailsでCORSのAccess-Control-Expose-Headersを設定する

More than 1 year has passed since last update.

問題

RailsとReactを使ってアプリを作っているのですが、ローカル環境で別々のサーバを立ててaxiosでRailsのAPIにアクセスしたところレスポンスのheadersが空でした。

解決方法

以下に書いてありました。

https://stackoverflow.com/questions/37897523/axios-get-access-to-response-header-fields

config/initializers/cors.rb
Rails.application.config.middleware.insert_before 0, Rack::Cors do
  allow do
    origins Rails.application.config.x.cors_allowed_origins

    resource '*',
             headers: :any,
             expose: ['access-token', 'uid'], # 追加
             methods: [:get, :post, :put, :patch, :delete, :options, :head]
  end
end

expose: ['access-token', 'uid']の部分を追加したところ、適切にheadersが返ってくるようになりました。
exposeの配列の中身はクライアント側で受け取りたいheadersになります。

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away