Ruby: How to use cookies

I did the same thing in PHP. How to use jquery-cookie Perl: How to use cookies PHP: How to use cookies Python3: How to use cookies

Send cookies

cookie_put.rb


#! /usr/bin/ruby
#
#	cookie_put.rb
#
#
puts "Set-Cookie: message='Good Morning';\n"
puts "Set-Cookie: aa='Good morning.';\n"
puts "Set-Cookie: bb='It's sunny today.';\n"
puts "Set-Cookie: cc='Hello';\n"
puts 'Content-Type: text/html',"\n"
puts "\n"

puts "<!DOCTYPE html>"
puts "<html lang=\"ja\">"
puts "<head>"
puts "<title>cookie put</title>"
puts "</head>"
puts "<body>"
puts "cookie_put.rb<p />"
puts "Aug/15/2020<p/>"
puts "</body>"
puts "</html>"

Check cookies

cookie_get.rb


#! /usr/bin/ruby
#
#	cookie_get.rb
#
cookie = Hash.new
ENV['HTTP_COOKIE'].split(/;\s/).each do |bo|
  mo = bo.split(/=/)
  cookie[mo[0]] = mo[1]
end

puts "Content-type: text/html\n\n"
puts "<!DOCTYPE html>"
puts "<html lang=\"ja\">"
puts "<head>"
puts "<meta http-equiv=\"CONTENT-TYPE\" content=\"text/html; charset=utf-8\" />"
puts "<title>Cookie Test</title></head>"
puts "<body>"

puts "Cookie Test<p />"
# puts cookie
cookie.each {|key, value|
	puts key,":",value,"<p />"
	}
puts "</body></html>"

Recommended Posts

Ruby: How to use cookies
How to use Ruby return
How to use Ruby on Rails
[Ruby] How to use any? Method
How to use Ruby inject method
How to use Map
How to use rbenv
[Ruby on Rails] How to use CarrierWave
How to use letter_opener_web
How to use with_option
How to use fields_for
How to use java.util.logging
How to use map
How to use collection_select
Ruby length, size, count How to use
How to use Twitter4J
How to use active_hash! !!
How to use MapStruct
How to use hidden_field_tag
How to use TreeSet
[How to use label]
How to use identity
How to use hashes
[Ruby] How to use slice for beginners
How to use JUnit 5
[Ruby on Rails] How to use redirect_to
[Ruby on Rails] How to use kaminari
How to use Dozer.mapper
How to use Gradle
Ruby: CSV :: How to use Table Note
How to use org.immutables
How to use java.util.stream.Collector
How to use VisualVM
How to use Map
[Ruby] How to use gsub method and sub method
[Ruby on Rails] How to use session method
[Ruby basics] How to use the slice method
[Java] How to use Map
How to use Chain API
How to use java Optional
How to use JUnit (beginner)
[Rails] How to use enum
How to use @Builder (Lombok)
[Swift] How to use UserDefaults
How to use java class
How to use Swift UIScrollView
[Ruby] How to comment out
How to use Big Decimal
[Java] How to use Optional ②
[Java] How to use removeAll ()
How to use String [] args
[Java] How to use string.format
How to use rails join
How to use Java Map
Rbenv command to use Ruby
How to use dependent :: destroy
How to use Eclipse Debug_Shell
How to use Apache POI
[Rails] How to use validation
How to use Java variables
[Ruby] How to write blocks