How to implement cookies in Ruby Net :: HTTP

What is my code.

Now I need to SEND the cookie to the host, but I can not find a solution.


def get_network_file(url=nil) begin http = Net::HTTP.new( @service_server, 80 ) resp, data = http.get( url, { "Accept-Language" => @locale } ) if resp.code.to_i != 200 RAILS_DEFAULT_LOGGER.error "*** return code != 200. code = #{resp.code}" return "" end rescue Exception => exc RAILS_DEFAULT_LOGGER.error "*** message --> #{exc.message}" return "" end return data end end 

+6
ruby cookies
source share
2 answers

You send cookies through the same hash that you send to the "Accept-Language" header, for example:

 resp, data = http.get( url, { "Accept-Language" => @locale, "Cookie" => "YOUR_COOKIE" } ) 

Most likely you need to grab the cookie first. See this for cookie handling examples.

+5
source

You must first extract the cookies from the server from the set-cookie server response header field (s). Once you have extracted the cookie (s), you will pass them / them to your client cookie header.

This question has already been asked in How to implement ruby ​​net / http cookie support?

The accepted answer is good if your server does not return a set of cookies, in which case you can look at fooobar.com/questions/123125 / ...

+2
source

All Articles