如何解析来自 POST 请求的数据

How to parse data from POST request

我有一个 getter 上下文:HTTP::Server::Context 和一个登录表单。
现在我想解析来自 context.request.body 的数据以获取用户输入的用户名和密码。

响应的内容类型:application/x-www-form-urlencoded

HTTP::Params.parse 是您要查找的内容:

# 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