10

Compojure does not bind the fields in a POST form. This is my route def:

(defroutes main-routes
  (POST "/query" {params :params}
    (debug (str "|" params "|"))
    "OK...")
)

When I post a form with fields in it, I get |{}|, i.e. there are no parameters. Incidentally, when I go http://localhost/query?param1=value1, params is not empty, and the values get printed on the server console.

Is there another binding for form fields??

George
  • 3,433
  • 4
  • 27
  • 25

3 Answers3

15

ensure you have input fields with name="zzz" attribute, but not only id="zzz".

html form collects all inputs and posts them using the name attribute

my_post.html

<form action="my_post_route" method="post">
    <label for="id">id</label> <input type="text" name="id" id="id" />
    <label for="aaaa">aaa</label> <input type="text" name="aaa" id="aaa" />
    <button type="submit">send</button>
</form>

my_routes.clj

(defroutes default-handler
  ;,,,,
  (POST "/my_post_route" {params :params} 
    (str "POST id=" (params "id") " params=" params))
  ;,,,,

produce response like

id=21 params={"aaa" "aoeu", "id" "21"}

zmila
  • 1,651
  • 1
  • 11
  • 13
4

This is a great example of how to handle parameters

(ns example2
  (:use [ring.adapter.jetty             :only [run-jetty]]
    [compojure.core                 :only [defroutes GET POST]]
    [ring.middleware.params         :only [wrap-params]]))

(defroutes routes
  (POST "/" [name] (str "Thanks " name))
  (GET  "/" [] "<form method='post' action='/'> What's your name? <input type='text' name='name' /><input type='submit' /></form>"))

(def app (wrap-params routes))

(run-jetty app {:port 8080})

https://github.com/heow/compojure-cookies-example

See under Example 2 - Middleware is Features

hawkeye
  • 34,745
  • 30
  • 150
  • 304
0

note: (params "id") return nil for me, i get a correct value with (params :id)

Damien Mattei
  • 358
  • 4
  • 9