Skip to content

Instantly share code, notes, and snippets.

@dotemacs
Created August 3, 2017 14:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dotemacs/05462cbb79f6190cd27fec1bbdea4ddd to your computer and use it in GitHub Desktop.
Save dotemacs/05462cbb79f6190cd27fec1bbdea4ddd to your computer and use it in GitHub Desktop.

How to parse JSON POST request ?

Vaniall macchiato app, created as per documentation

lein new macchiato first

The request looks like this:

curl -H "Content-Type: application/json" \
-H "Accept: application/json" \
-X POST -d '{"foo":"bar"}' http://localhost:3000/bar

The bar function looks like this

(defn bar [req res raise]
  (println req)
  (-> "bar"
      (r/ok)
      (r/content-type "application/json")
      (res)))

route looks like

“/bar” {:post bar}

the middleware looks like

(defaults/wrap-defaults handler defaults/api-defaults)

What do I need to do in order to be able to parse the body of the request?

Doing something like this:

(js/JSON.parse (:body req))

complains about

SyntaxError: Unexpected token o in JSON at position 1

Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment