问问compojure里面处理POST路由哪里出错了

这几天不是正在学ring和compojure吗,又复习了一下html,我就想让ring去处理POST请求

(ns form.core
  (:require [ring.adapter.jetty :as jetty]
            [ring.util.http-response :as resp]
            [ring.util.response :refer [redirect]]
            [ring.middleware.reload :refer [wrap-reload]]
            [compojure.core :refer [routes GET defroutes POST]]
            [clojure.java.io :as io]
            ))            

(defroutes app
  (GET "/login" request (io/resource "form.html"))
  (POST "/form" [fname lname] (str "first name is " fname ", " "last name is " lname)))

(defn -main []
  (jetty/run-jetty
   (wrap-reload app)
   {:port 3000
    :join? false}))

这是我在resouce目录下的form.html

<html>
  <head>

  </head>

  <body>
    <form action="/form" method="post">
      <input type="text" name="fname"><br>
      <input type="text" name="lname"><br>
      <input type="submit" value="submit"><br>
  </body>
  </form>

可是我用curl处理的时候,发现

url=localhost:3000/form
curl $url -X POST -d 'fname=hello&lname=world' 

得到的响应是

first name is ,  last name is

路由里怎么接受不到POST的数据啊,求助


我的依赖文件project.clj

(defproject form "0.1.0-SNAPSHOT"
  :description "FIXME: write description"
  :url "http://example.com/FIXME"
  :license {:name "Eclipse Public License"
            :url "http://www.eclipse.org/legal/epl-v10.html"}
  :dependencies [
                 [org.clojure/clojure "1.10.0"]
                 [ring "1.6.3"]
                 [metosin/ring-http-response "0.9.0"]
                 [compojure "1.6.1"]
                 [ring/ring-defaults "0.3.2"]
                 ]
  :repositories [["central" "http://maven.aliyun.com/nexus/content/groups/public"]
                 ["clojars" "https://mirrors.tuna.tsinghua.edu.cn/clojars/"]]

  :main form.core)

(require 'cemerick.pomegranate.aether)
(cemerick.pomegranate.aether/register-wagon-factory!
 "http" #(org.apache.maven.wagon.providers.http.HttpWagon.))

Clojure的问题不是应该在clojure-china论坛问么?虽然这里问也无不可。

那个论坛上不去了

用一个function做handler,request -> response

(defn form-handler [request] (prn request) {:status 200, :body "ok!"})

然后路由这里

(POST "/form" _ form-handler)

然后看看打印内容应该就都明确了。


PS, 传app的地方用#'app, 传Var的话可以让你每次重定义app之后不用重启服务。

使用wrap-params把form body里面的内容解析出来