The title is pretty self-explanatory. Is there any way to get the headers (except for Rack::Request.env[]
)?
相关问题
- Angular RxJS mergeMap types
- Google Apps Script: testing doPost() with cURL
- How to instantiate Http service in main.ts manuall
- C#使用http访问网络,有办法用指定网卡访问网络嘛?
- How to specify memcache server to Rack::Session::M
相关文章
- C#使用http访问网络,有办法用指定网卡访问网络嘛?
- Ruby using wrong version of openssl
- Is there a size limit for HTTP response headers on
- Difference between Thread#run and Thread#wakeup?
- how to call a active record named scope with a str
- “No explicit conversion of Symbol into String” for
- Segmentation fault with ruby 2.0.0p247 leading to
- Is a unicode user agent legal inside an HTTP heade
The HTTP headers are available in the Rack environment passed to your app:
So the HTTP headers are prefixed with "HTTP_" and added to the hash.
Here's a little program that extracts and displays them:
When I run this, in addition to the HTTP headers as shown by Chrome or Firefox, there is a "VERSION: HTPP/1.1" (i.e. an entry with key "HTTP_VERSION" and value "HTTP/1.1" is being added to the env hash).
Like @Gavriel's answer, but using
transform_keys
(simpler):You can even make it so lookups still work even if the case is different:
So for example, even if
headers
normalizes the keys so it returns this:you can still look up headers using the more natural/common names for these headers:
Based on @matt's answer, but this really gives you the request headers in a hash as requested in the question:
Depending on what key convention you prefer you might want to use something else instead of :capitalize.