Skip to content

Instantly share code, notes, and snippets.

@andrewhr
Forked from edipofederle/gist:84ebea07db68021cedaa
Last active August 29, 2015 14:27
Show Gist options
  • Save andrewhr/98c3e44581e54988bc09 to your computer and use it in GitHub Desktop.
Save andrewhr/98c3e44581e54988bc09 to your computer and use it in GitHub Desktop.
Comparado ao original, foi adicionado o middleware the EDN, já que o ring-defaults não inclui nenhuma serialização por padrão. No lado do cliente removi o reader extra e exibido apenas a string retornada. Isso se deve por que no servidor é retornado como "Params: ...", o que não é um EDN válido, quebrando a deserialização
;; Testando somente o server side funciona OK.
curl -X POST -d "foo=bar" http://localhost:10555/posts
Params: {:foo "bar"}%
;; Client Side
(def ^:private meths
{:get "GET"
:put "PUT"
:post "POST"
:delete "DELETE"})
(defn edn-xhr [{:keys [method url data on-complete]}]
(let [xhr (XhrIo.)]
(events/listen xhr goog.net.EventType.COMPLETE
(fn [e]
(on-complete (.getResponseText xhr))))
(. xhr
(send url (meths method) (when data (pr-str data))
#js {"Content-Type" "application/edn"}))))
;; The request:
(edn-xhr
{:method :post
:url (str "posts")
:data {:name "Foo Bar"}
:on-complete
(fn [res]
(println "server response:" res))})
)
;; Server side
(def http-handler
(let [handler (-> routes
(wrap-defaults api-defaults)
(wrap-edn-params))] ;; fogus/ring-edn 0.3.0
(if is-dev? (reload/wrap-reload handler) handler)))
(defroutes routes
(resources "/")
(POST "/posts" {params :params} {:status 200 :body (str "Params: " params)})
(resources "/react" {:root "react"})
(GET "/*" req (page)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment