使用Rack-cors和Grape添加自定义响应头 [英] Add custom response header with Rack-cors and Grape
问题描述
我正在使用Ruby on Rails API开发Ionic(Cordova)应用程序.
我想使用响应头在登录后返回令牌.
我正在使用rack-cors
gem来执行跨源请求:
I'm developing a Ionic(Cordova) app with a Ruby on Rails API.
I want to use response headers to return a token after login.
I'm using rack-cors
gem to make Cross Origin Request work:
config.middleware.insert_after Rails::Rack::Logger, Rack::Cors, :logger => Rails.logger do
allow do
origins '*'
resource '/api/*', :headers => :any, :methods => [:get, :post, :options, :put]
end
end
和葡萄宝石来管理我的API路由.
但是自从我添加rack-cors
以来,我无法找到一种在响应中添加标头的方法.
and grape gem to manage my API routes.
But i can't find a way to add a header to my response since i added rack-cors
.
我尝试过:
header('Access-Token', user.token.key)
但是它不起作用.无论我做什么,我最终都会得到这些标题:
But it doesn't work. Whatever i do i end up with those headers:
{缓存控制:最大年龄= 0,私有,必须重新验证",内容类型: " application/json'}
{cache-control: "max-age=0, private, must-revalidate", content-type: "application/json"}
有人可以帮助我解决这个问题吗?
Can anyone help me with this issue ?
推荐答案
我使用了gem 'devise_token_auth'
此外,我在application.rb中有此配置.
Also, i had this configuration in application.rb.
class Application < Rails::Application
# Do not swallow errors in after_commit/after_rollback callbacks.
config.active_record.raise_in_transactional_callbacks = true
config.middleware.use Rack::Cors do
allow do
origins '*'
resource '*',
:headers => :any,
:expose => ['access-token', 'expiry', 'token-type', 'uid', 'client'],
:methods => [:get, :post, :options, :delete, :put]
end
end
end
这篇关于使用Rack-cors和Grape添加自定义响应头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!