Reputation:
I have a getter context : HTTP::Server::Context and a login form.
Now I want to parse data from context.request.body to get username and password which user input.
The response's content type: application/x-www-form-urlencoded
Upvotes: 2
Views: 1366
Reputation: 15620
HTTP::Params.parse
is what you're looking for:
# Based on the sample code in https://crystal-lang.org/ home
require "http/server"
server = HTTP::Server.new(8080) do |context|
context.response.content_type = "text/plain"
if body = context.request.body
params = HTTP::Params.parse(body)
context.response.print "Hello #{params["user"]? || "Anonymous"}!"
else
context.response.print "You didn't POST any data :("
end
end
puts "Listening on http://127.0.0.1:8080"
server.listen
Upvotes: 4