Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Missing form parameters in Compojure POST request

I'm having problems getting the form parameters in the following Compojure example:

(ns hello-world
  (:use compojure.core, ring.adapter.jetty)
  (:require [compojure.route :as route]))

(defn view-form []
(str "<html><head></head><body>"
   "<form method=\"post\">"
   "Title <input type=\"text\" name=\"title\"/>"
   "<input type=\"submit\"/>"
   "</form></body></html>"))

(defroutes main-routes
  (GET "/" [] "Hello World")
  (GET "/new" [] (view-form))
  (POST "/new" {params :params} (prn "params:" params))
  (route/not-found "Not Found"))

(run-jetty main-routes {:port 8088})

When submitting the form the output is always

params: {}

and I can't figure out why the title parameter is not in the params map.

I'm using Compojure 0.6.2.

like image 362
cretzel Avatar asked May 17 '11 20:05

cretzel


2 Answers

Have you taken into account this:

As of version 0.6.0, Compojure no longer adds default middleware to routes. This means you must explicitly add the wrap-params and wrap-cookies middleware to your routes.

Source: https://github.com/weavejester/compojure

I tried your example with my current setup and it worked. I have included the following: require [compojure.handler :as handler] and (handler/api routes).

like image 172
ponzao Avatar answered Nov 02 '22 20:11

ponzao


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

like image 35
hawkeye Avatar answered Nov 02 '22 21:11

hawkeye