CrossClj

0.9.0 docs

SourceDocs



RECENT

    ring-http-response

    Clojars

    May 10, 2017


    OWNER
    Metosin
    Tampere, Helsinki, Finland
    www.metosin.fi

    Readme

    Index of all namespaces


    « Project + dependencies

    Handling HTTP Statuses with Clojure(Script)

    ring.util.http-responseDocsSource

    VARS

    A accepted already-reported

    B bad-gateway bad-gateway! bad-request bad-request! bandwidth-limit-exceeded bandwidth-limit-exceeded! blocked-by-windows-parental-controls blocked-by-windows-parental-controls!

    C charset conflict conflict! content-type continue created

    E enhance-your-calm enhance-your-calm! expectation-failed expectation-failed!

    F failed-dependency failed-dependency! file-response find-header forbidden forbidden! found

    G gateway-timeout gateway-timeout! get-header gone gone!

    H header http-version-not-supported http-version-not-supported!

    I im-used insufficient-storage insufficient-storage! internal-server-error internal-server-error!

    L length-required length-required! locked locked! loop-detected loop-detected!

    M method-not-allowed method-not-allowed! moved-permanently multi-status multiple-choices

    N network-authentication-required network-authentication-required! network-connect-timeout network-connect-timeout! network-read-timeout network-read-timeout! no-content non-authoritative-information not-acceptable not-acceptable! not-extended not-extended! not-found not-found! not-implemented not-implemented! not-modified

    O ok

    P partial-content payment-required payment-required! permanent-redirect precondition-failed precondition-failed! precondition-required precondition-required! processing proxy-authentication-required proxy-authentication-required!

    R request-entity-too-large request-entity-too-large! request-header-fields-too-large request-header-fields-too-large! request-timeout request-timeout! request-uri-too-long request-uri-too-long! requested-range-not-satisfiable requested-range-not-satisfiable! reset-content resource-data resource-response response? retry-with retry-with!

    S see-other service-unavailable service-unavailable! set-cookie status switching-protocols

    T temporary-redirect throw! too-many-requests too-many-requests!

    U unauthorized unauthorized! unavailable-for-legal-reasons unavailable-for-legal-reasons! unordered-collection unordered-collection! unprocessable-entity unprocessable-entity! unsupported-media-type unsupported-media-type! update-header upgrade-required upgrade-required! url-response use-proxy

    V variant-also-negotiates variant-also-negotiates!

    The README below is fetched from the published project artifact. Some relative links may be broken.

    ring-http-response Build Status Downloads Dependencies Status

    Handling HTTP Statuses with Clojure(Script), originally ported from the awesome Spray. Mostly a drop-in-place replacement for ring.util.response.

    Latest version

    Clojars Project

    Usage

    Generating HTTP responses

    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 ""}
    

    Asserting HTTP responses

    Available for both Clojure & ClojureScript.

    (require '[ring.util.http-predicates :as predicates])
    
    (predicates/ok? {:status 200})
    ; true
    
    (predicates/not-found? {:status 404})
    ; true
    

    Status codes & documentation

    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."}
    

    Throwing error responses

    All response functions for HTTP error response have a exception throwing sibling with a bang in the name (bad-request & bad-request!). These functions Slingshot 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 slingshot 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"}}
    

    Catching thrown HTTP responses

    Middleware ring.middleware.http-response/wrap-http-response catches thrown HTTP-responses and returns the responses within. See the facts for examples.

    Migrating from ring.util.response

    1. add the dependency
    2. change your imports from ring.util.response to ring.util.http-response
    3. convert your responses to use the correct HTTP-terms:
    • 200: response => ok
    • 302: redirect => found
    • 303: redirect-after-post => see-other
    1. enjoy

    created 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.

    License

    Original code: Copyright © 2011-2013 the spray project http://spray.io.

    Copyright © 2014-2017 Metosin Oy

    Distributed under the Eclipse Public License, the same as Clojure.