Skip to content

Instantly share code, notes, and snippets.

@mrk21
Last active August 31, 2019 10:01
Show Gist options
  • Save mrk21/7831ddef0501460e65f6c4594b5be68b to your computer and use it in GitHub Desktop.
Save mrk21/7831ddef0501460e65f6c4594b5be68b to your computer and use it in GitHub Desktop.
Extract request HTTP headers for Rails
class ApplicationController < ActionController::Base
private
# @see [Rack::RequestでHTTPリクエストヘッダ一覧を書き出す - Qiita](https://qiita.com/mechamogera/items/9c620155e669b394d513)
# @example
# {
# "Version"=>"HTTP/1.1",
# "Host"=>"localhost:3000",
# "User-Agent"=>"curl/7.54.0",
# "Accept"=>"*/*",
# "X-Forwarded-For"=>"192.0.0.1"
# }
def request_http_header
return @request_http_header if @request_http_header.present?
@request_http_header = request.headers.env.select { |k, _| k =~ /^HTTP_/ }
@request_http_header = @request_http_header.reduce({}) do |r, (k, v)|
name = k
.sub(/^HTTP_/, '')
.downcase
.split('_')
.map(&:camelize)
.join('-')
r[name] = v
r
end
@request_http_header
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment