中访问自定义标头变量

中访问自定义标头变量

本文介绍了在 Ruby on Rails 中访问自定义标头变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

阅读以下问题后(.

现在自定义变量总是以 HTTP_ 和 ,除了对于 CGI 变量.

HTH

再次检查,变量中的 - 被替换为 _ 并以 HTTP_ 开头.在上面的链接中,检查行号 91-94:

key = key.upcase.tr('-', '_')

After reading the following question (Authorization header in Ruby on Rails accessed with key HTTP_AUTHORIZATION instead of Authorization?) I have a similar problem as the OP, but the proposed answer does not seem to solve mine.

I define a custom header as such in a call to my locally hosted server (through Postman):

@Igor: I actually use Postman, so I just added the curl code to demonstrate what I did. I guess it would be better to include a screenshot:

And this is the code in my controller which tries to read said header:

def authenticate_through_header
  custom_header_value = request.headers['custom_header']
end

However, this return nil. On the other hand, request.headers['HTTP_CUSTOM_HEADER'] returns the value. According to the question I linked to initially, I should be able to get the value through passing the name within the brackets [ ] - is this something which has been changed in newer Rails versions?

Cheers :-)

Update: It also works to access the variable in the following way: request.headers['custom-header']. So apparently it works to replace the underscore with a hyphen, which seems weird.

解决方案

Yes, it has changed in Rails 4. Take a look at the Http::Headers code.

Now custom variables are always prepended with HTTP_ and , except for CGI variables.

HTH

EDIT: Just checked again, - in variables are getting replaced with _ and being prepended with HTTP_. In above link, check line number 91-94:

key = key.upcase.tr('-', '_')

这篇关于在 Ruby on Rails 中访问自定义标头变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 06:43