Last active
August 29, 2015 14:15
-
-
Save jbarber/143351e2db1bebb4c910 to your computer and use it in GitHub Desktop.
Example of liberator resource with post redirect and setting flash
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ns user | |
(:require [liberator core representation] | |
[ring.middleware params session flash] | |
[ring.mock.request])) | |
;; Return HTTP 303 - have to manage Location ourselves | |
((-> | |
(liberator.core/resource | |
:available-media-types ["text/html" "application/x-www-form-urlencoded"] | |
:allowed-methods [:get :post] | |
:post! (fn [ctx] ["true/false from post is ignored (context isn't)" {:post-output ::bar}]) | |
:handle-ok (fn [ctx] (str "get handled" (get-in ctx [:request :flash]))) | |
:post-redirect? {:location "/"} | |
:handle-see-other (fn [ctx] | |
(liberator.representation/ring-response | |
{:flash ::submitted | |
:headers {"Location" (ctx :location)}}))) | |
ring.middleware.params/wrap-params | |
ring.middleware.session/wrap-session | |
ring.middleware.flash/wrap-flash | |
; (liberator.dev/wrap-trace :header :ui) | |
) | |
(ring.mock.request/request :post "/any" {:email "foo@example.com"})) | |
;; Returns 201 - Location managed by liberator | |
((-> | |
(liberator.core/resource | |
:available-media-types ["text/html" "application/x-www-form-urlencoded"] | |
:allowed-methods [:get :post] | |
:post! (fn [ctx] ["true/false from post is ignored (context isn't)" {:post-output ::bar}]) | |
:handle-ok (fn [ctx] (str "get handled" (get-in ctx [:request :flash]))) | |
:post-redirect? [false {:location "/"}] | |
:handle-created (fn [ctx] | |
(liberator.representation/ring-response | |
{:flash ::submitted}))) | |
ring.middleware.params/wrap-params | |
ring.middleware.session/wrap-session | |
ring.middleware.flash/wrap-flash) | |
(ring.mock.request/request :post "/any" {:email "foo@example.com"})) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment