Fork me on GitHub
#yada
<
2018-04-14
>
thegeez10:04:03

@stijn generating a body from xml:

thegeez10:04:19

(extend-protocol yada.body/MessageBody
  clojure.data.xml.Element
  (to-body [cdxe representation]
    (clojure.data.xml/emit-str cdxe))
  (content-length [out]
    nil ;; let default logic get length of string body
    ))

(defn make-server []
  (yada/listener
   ["/" (yada/resource
         {:methods
          {:get
           {:produces #{"application/xml"}
            :response
            (fn [ctx]
              #clojure.data.xml.Element{:tag :p,
                                        :attrs {},
                                        :content ["hello "
                                                  #clojure.data.xml.Element{:tag :b,
                                                                            :attrs {},
                                                                            :content ["world"]}]})}}})]
   {:port 3000}))

;; curl -vv "" -H "Accept: application/xml"
;; > GET / HTTP/1.1
;; > Accept: application/xml
;; > 
;; < HTTP/1.1 200 OK
;; < Content-Type: application/xml
;; < content-length: 63
;; < 
;; <?xml version="1.0" encoding="UTF-8"?><p>hello <b>world</b></p>