English 中文(简体)
我如何用http://www.get?
原标题:How do I preserve case with http.get?

我要求派遣一名吉大港定居人士担任特定性质的职务。 我知道这违反了《刑法》,但我有要求。

<代码>.get似乎改变了第一行长提供的情况。 我怎么能够保护特征?

最佳回答

根据Tin Man的回答,Net:HTTP Library is calls#down> on theirtom Header key (and all Header keys),这里还有一些其他选择,这些选择不构成整个。 净额:HTTP

你可以尝试:

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

避免清理方法藏匿点,或者将上述结果储存在一级不变:

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

a. 试图推翻这一特定行为:

class StringWithIdentityDowncase < String
  def downcase
    self
  end
end

custom_header_key = StringWithIdentityDowncase.new("X-miXEd-cASe")
问题回答

地雷是这样做的一种方式,但我建议以“@yfeldblum”建议的方式行事,简单地说,为需要将其案件分开的头盔提供短路条码<>。


在网上多个地方: HTTP:HTTPHeader 头盔使用<代码>downcase<>code>。

我认为,改变这一行为是极其严重的,但这样做是可能的。 将这添加到您的消息来源,并将重新界定HTTPHeader模块中的“编码”栏目<<>条码/代码”。

module HTTPHeader

  def initialize_http_header(initheader)
    @header = {}
    return unless initheader
    initheader.each do |key, value|
      warn "net/http: warning: duplicated HTTP header: #{key}" if key?(key) and $VERBOSE
      @header[key] = [value.strip]
    end
  end

  def [](key)
    a = @header[key] or return nil
    a.join( ,  )
  end

  def []=(key, val)
    unless val
      @header.delete key
      return val
    end
    @header[key] = [val]
  end

  def add_field(key, val)
    if @header.key?(key)
      @header[key].push val
    else
      @header[key] = [val]
    end
  end

  def get_fields(key)
    return nil unless @header[key]
    @header[key].dup
  end

  def fetch(key, *args, &block)   #:yield: +key+
    a = @header.fetch(key, *args, &block)
    a.kind_of?(Array) ? a.join( ,  ) : a
  end

  # Removes a header field.
  def delete(key)
    @header.delete(key)
  end

  # true if +key+ header exists.
  def key?(key)
    @header.key?(key)
  end

  def tokens(vals)
    return [] unless vals
    vals.map {|v| v.split( , ) }.flatten
    .reject {|str| str.strip.empty? }
    .map {|tok| tok.strip }
  end

end

我认为,这是实现这一目的的有力途径,但更不用说气.。

虽然这应该为使用“网”的任何鲁比图书馆确定问题,但对于使用库勒或校准的宝石来说,这个问题可能不会解决。

Joshua Cheek s answer is great, but it does in work anymore in Ruby 2.3

这一修改规定:

class Net::HTTP::ImmutableHeaderKey
  ...

  def to_s
    caller.first.match(/capitalize/) ? self : @key
  end
end

It all falls down into the net/generic_request#write_header. You could monkey patch the code

#  net/generic_request  line 319
def write_header(sock, ver, path)
  customheaders = {
    "My-Custom-Header" => "MY-CUSTOM-HEADER",
    "Another-Custom-Header" => "aNoThErCuStOmHeAdEr"
  }
  buf = "#{@method} #{path} HTTP/#{ver}
"
  each_capitalized do |k,v|
    customheaders.key?(k) ? kk = customheaders[k] : kk = k
    buf << "#{kk}: #{v}
"
  end
  buf << "
"
  sock.write buf
end

and you don t need to rewrite the whole net/http/header, net/generic_request and net/http chain. It s not the best solution, but it s the easiest one I guess and there s least amount of monkey patching.

希望会有所助益。





相关问题
rails collection_select vs. select

collection_select and select Rails helpers: Which one should I use? I can t see a difference in both ways. Both helpers take a collection and generates options tags inside a select tag. Is there a ...

SSL slowness in EC2

We ve deployed our rails app to EC2. In our setup, we have two proxies on small instances behind round-robin DNS. These run nginx load balancers for a dynamically growing and shrinking farm of web ...

Auth-code with A-Za-z0-9 to use in an URL parameter

As part of a web application I need an auth-code to pass as a URL parameter. I am currently using (in Rails) : Digest::SHA1.hexdigest((object_id + rand(255)).to_s) Which provides long strings like : ...

RubyCAS-Client question: Rails

I ve installed RubyCAS-Client version 2.1.0 as a plugin within a rails app. It s working, but I d like to remove the ?ticket= in the url. Is this possible?

activerecord has_many :through find with one sql call

I have a these 3 models: class User < ActiveRecord::Base has_many :permissions, :dependent => :destroy has_many :roles, :through => :permissions end class Permission < ActiveRecord::...

Ordering a hash to xml: Rails

I m building an xml document from a hash. The xml attributes need to be in order. How can this be accomplished? hash.to_xml

Text Editor for Ruby-on-Rails

guys which text editor is good for Rubyonrails? i m using Windows and i was using E-Texteditor but its not free n its expired now can anyone plese tell me any free texteditor? n which one is best an ...

How to get SQL queries for each user where env is production

I’m developing an application dedicated to generate statistical reports, I would like that user after saving their stat report they save sql queries too. To do that I wrote the following module: ...

热门标签