cmal
cmal

Reputation: 2222

How to get the content of `HttpInputOverHTTP` in Clojure Compojure/Ring project?

How to get the content of an incoming POST http request's :body #object[org.eclipse.jetty.server.HttpInputOverHTTP 0x42c3599b "HttpInputOverHTTP@42c3599b"] in a Compojure/Ring project?

I know that this :body is composed of a part named data whose MIME-type is text-plain and another part named excel whose MIME-type is application/excel.

I slurped the content of :body and it shows: enter image description here

Upvotes: 5

Views: 2616

Answers (3)

Jp_
Jp_

Reputation: 6183

I was seeing it in Reitit, what fixed for me was to change the order of the middlewares so the exception-middleware is after the multipart/multipart-middleware.

:middleware [;; multipart
             multipart/multipart-middleware
             ;; exception handling
             exception-middleware]

Upvotes: 0

Ivan Grishaev
Ivan Grishaev

Reputation: 1681

Parsing a binary stream manually would be difficult. Wrap your handler as follows:

(wrap-multipart-params handler options)

This middleware parses the body and populates :params parameters with parsed data as well.

See ring.middleware.multipart-params documentation for more details.

Upvotes: 5

Alan Thompson
Alan Thompson

Reputation: 29958

You can find a basic example in the Clojure Cookbook (O'Reilly), which I highly recommend:

(ns ringtest
  (:require
    [ring.adapter.jetty :as jetty]
    clojure.pprint))

;; Echo (with pretty-print) the request received
(defn handler [request]
  {:status 200
   :headers {"content-type" "text/clojure"}
   :body (with-out-str (clojure.pprint/pprint request))})

(defn -main []
  ;; Run the server on port 3000
  (jetty/run-jetty handler {:port 3000}))

Upvotes: -1

Related Questions