Created
April 17, 2015 21:57
-
-
Save mayfer/eb541764608c703938c7 to your computer and use it in GitHub Desktop.
Demonstrating HTTP concepts with sinatra
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'sinatra' | |
post '/' do | |
response.set_cookie("username", params[:username]) | |
redirect '/' | |
end | |
get '/logout' do | |
response.set_cookie("username", value: "test", expires: Time.now ) | |
redirect '/' | |
end | |
# when a browser requests "/" with method GET | |
get '/' do | |
# respond with this HTTP response body | |
# puts response.headers.inspect | |
# response.headers["Content-Type"] = "text/plain" | |
# puts params.inspect | |
cookie = request.cookies["username"] | |
if params[:username] | |
title = "hello #{params[:username]}" | |
elsif cookie | |
title = cookie | |
else | |
title = "somthin else" | |
end | |
return <<-eos | |
<html> | |
<head> | |
<title>HTTP example</title> | |
</head> | |
<body> | |
<h1>#{title}</h1> | |
<form action="/" method="POST"> | |
<input type="text" name="username" placeholder="username" /> | |
<input type="password" name="password" placeholder="password" /> | |
<input type="submit" value="Send" /> | |
</form> | |
</body> | |
</html> | |
eos | |
end | |
get "/:any" do | |
status 404 | |
"error!! not found" | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment