如何使用 http.get 保留大小写? [英] How do I preserve case with http.get?

查看:47
本文介绍了如何使用 http.get 保留大小写?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要以特定的字符大小写发送 HTTP 标头.我知道这违反了 RFC,但我有一个要求.

I have a requirement to send an HTTP header in a specific character-case. I am aware that this is against the RFC, but I have a requirement.

http.get 似乎改变了我提供的标题字典的大小写.如何保留字符大小写?

http.get seems to change the case of the headers dictionary I supply it. How can I preserve the character-case?

推荐答案

基于 Tin Man 的回答,即 Net::HTTP 库正在调用 #downcase自定义标头键(和所有标头键),这里有一些附加选项,它们不会修补整个 Net::HTTP.

Based on the Tin Man's answer that the Net::HTTP library is calling #downcase on your custom header key (and all header keys), here are some additional options that don't monkey-patch the whole of Net::HTTP.

你可以试试这个:

custom_header_key = "X-miXEd-cASe"
def custom_header_key.downcase
  self
end

为避免清除方法缓存,请将上述结果存储在类级常量中:

To avoid clearing the method cache, either store the result of the above in a class-level constant:

custom_header_key = "X-miXEd-cASe"
def custom_header_key.downcase
  self
end
CUSTOM_HEADER_KEY = custom_header_key

或子类 String 以覆盖该特定行为:

or subclass String to override that particular behavior:

class StringWithIdentityDowncase < String
  def downcase
    self
  end
end

custom_header_key = StringWithIdentityDowncase.new("X-miXEd-cASe")

这篇关于如何使用 http.get 保留大小写?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆