2

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

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

4

1 回答 1

4

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
于 2016-12-02T22:42:56.993 回答