The accepted answer is incorrect. CGI::Cookie#to_s generates a string that should be sent by SERVER to the client, and not what Net :: HTTP should use. This is easy to demonstrate:
[1] pry(main)> require 'cgi' => true [2] pry(main)> CGI::Cookie.new('usr', 'value').to_s => "usr=value; path="
Code like this should work better.
require 'net/http' require 'cgi' uri = URI("http://httpbin.org/cookies") http = Net::HTTP.new(uri.host, uri.port) request = Net::HTTP::Get.new(uri.request_uri) request['Cookie'] = "usr=#{CGI.encode cookie_value}" r = http.request(request) puts r.body
Or if you have multiple cookies in a hash:
h = {'cookie1' => 'val1', 'cookie2' => 'val2'} req['Cookie'] = h.map { |k,v| "#{k}=#{CGI.encode v}" } .join('; ')
Paladin
source share