0

I am able to access api when try wit curl cmd, but same not able to access with net/http,

I did the string patch as per this link Preserving case in HTTP headers with Ruby's Net:HTTP and it was working well with ruby 2.2.4,

but recently we migrated to ruby 2.6.5 version, since then we are facing this issue again.

can anyone could help me with it.

sample code

token = JSON.parse(token_id)
  request = Net::HTTP::Get.new(uri)
  sso_token = token["headers"][0]["value"][0]
  request[CaseSensitiveString.new('sso_token')] = sso_token
  request["Accept"] = "application/json"
  req_options = {
      use_ssl: uri.scheme == "https"
  }
  response = Net::HTTP.start(uri.hostname, uri.port, req_options) do |http|
    http.request(request)
  end
  response.code
  response.body

class CaseSensitiveString < String
def downcase
  self
end
def capitalize
  self
end

end

  • Can you add some of your code for us to get a better idea of your setup? – bwalshy Jul 23 '20 at 13:08
  • Hi @bwalshy I have added the sample code, could you please check it – Radha Krishna Jul 23 '20 at 13:52
  • Does this answer your question? [How to force Ruby to respect underscore in Net::HTTP header](https://stackoverflow.com/questions/60838996/how-to-force-ruby-to-respect-underscore-in-nethttp-header) – anothermh Jul 23 '20 at 19:54
  • @anothermh - it requires curl lib.dll for http requests, could you please provide with net/http which would help for us – Radha Krishna Jul 24 '20 at 03:55
  • As the accepted answer at the linked question points out, there is no reliable, long-term solution for Net::HTTP to allow this. The correct answer is to use something that does not require extra steps, ugly hacks, or monkey patching. – anothermh Jul 24 '20 at 04:54

2 Answers2

0

Sorry, I needed to patch net/http as we have large existing project and its working with below code for ruby 2.5 and above

module Net::HTTPHeader 
    def capitalize(name)
     name 
    end 
    private :capitalize 
end
0

Experienced this issue now with ruby 3.0.6. Looking through the Net::HTTP::Header code, I found out that a split is being done on the header (https://github.com/wycats/net-http/blob/master/lib/net2/http/header.rb#LL177C13-L177C13). Therefore, CaseSensitiveString should now implement a split method, yielding an array of CaseSensitiveString:

class CaseSensitiveString < String
  def downcase
    self
  end

  def capitalize
    self
  end

  def split(arg)
    super(arg).map { |str| CaseSensitiveString.new(str) }
  end

  def to_s
    self
  end
end
user3592693
  • 181
  • 2
  • 4