I have two rails applications App1 and App2 both running on separate URLs but on same machine. App1 fetch some data using Net::HTTP from App2. What I need to do is to set a cookie in App2 when request is made from App1. Currently, it’s not setting the cookie. Do I need to add some header in App1 while sending request to App2, or what?
Here is the code to fetch the content:
def get_content(url)
uri = URI.parse(url)
params = Hash[*uri.query.split("&").map {|part| part.split("=") }.flatten]
http = Net::HTTP.new(uri.host, uri.port)
request = Net::HTTP::Get.new(uri.path)
request.set_form_data( params )
request = Net::HTTP::Get.new( uri.path+ '?' + request.body )
if uri.scheme == "https" # enable SSL/TLS
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
end
http.start do
http.request(request) do|res|
return res.body
end
end
end
Please advice.
first question: Where do you want to place the cookie? On the client who is browsing your website?
Is your request flow like:
Client –[web browser]–> App 1 –[net::http]–> App2
If this is the flow, you have to proxy the cookie:
Make sure you require CGI::Cookie
Here are the docs:
http://www.ruby-doc.org/stdlib/libdoc/net/http/rdoc/classes/Net/HTTPHeader.html#M001307
http://ruby-doc.org/stdlib/libdoc/cgi/rdoc/classes/CGI/Cookie.html#M000170