Handling HTTP Statuses with Clojure(Script), originally ported from the awesome Spray.
Mostly a drop-in-place replacement for ring.util.response.
Functions take either a body, url, both or nothing as parameters in align to the spec.
(require '[ring.util.http-response :refer :all])
(continue)
; {:status 100, :headers {}, :body ""}
(ok "body")
; {:status 200, :headers {}, :body "body"}
(created "url" "body")
; {:status 201, :headers {"Location" "url"}, :body "body"}
(found "url")
; {:status 302, :headers {"Location" "url"}, :body ""}
Available for both Clojure & ClojureScript.
(require '[ring.util.http-predicates :as predicates])
(predicates/ok? {:status 200})
; true
(predicates/not-found? {:status 404})
; true
For referring HTTP codes by name & for api docs like Swagger.
(require '[ring.util.http-status :as status])
status/ok
; 200
status/not-found
; 404
(status/get-name 404)
; "Not Found"
(status/get-description 404)
; "The requested resource could not be found but may be available again in the future."
(status/status 404)
; {:name "Not Found"
; :description "The requested resource could not be found but may be available again in the future."}
All response functions for HTTP error response have a exception throwing sibling with a bang in the name
(bad-request & bad-request!). These functions throw the
response wrapped in an ExceptionInfo.
(bad-request "fail")
; {:status 400, :headers {}, :body "fail"}
(bad-request! "fail")
; clojure.lang.ExceptionInfo: throw: {:type :ring.util.http-response/response, :response {:status 400, :headers {}, :body "fail"}}
There is also a throw! function to throw any kind response in an exception.
(throw! (header (bad-request "body") "header" "value"))
; clojure.lang.ExceptionInfo: throw: {:type :ring.util.http-response/response, :response {:status 400, :headers {"header" "value"}, :body "body"}}
Middleware ring.middleware.http-response/wrap-http-response catches thrown HTTP-responses and returns the responses within.
See the facts for examples.
ring.util.response to ring.util.http-responseresponse => okredirect => foundredirect-after-post => see-othercreated and not-found are same in both packages. Also rest of the public vars in ring.util.response are available via ring.util.http-response.
These include: status, header, file-response, content-type, find-header, get-header, update-header, charset, set-cookie, response?
resource-data, url-response and resource-response.
CHANGELOG.md and increment the version number in project.cljOriginal code: Copyright © 2011-2013 the spray project http://spray.io.
Copyright © 2014-2021 Metosin Oy
Distributed under the Eclipse Public License, the same as Clojure.
Can you improve this documentation? These fine people already did:
Tommi Reiman, Juho Teperi, Joel Kaasinen, Quan Nguyen, Miikka Koskinen, Anton Podviaznikov & Wu JiangEdit on GitHub
cljdoc builds & hosts documentation for Clojure/Script libraries
| Ctrl+k | Jump to recent docs |
| ← | Move to previous article |
| → | Move to next article |
| Ctrl+/ | Jump to the search field |