This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2023-11-04
Channels
- # announcements (5)
- # babashka (2)
- # beginners (53)
- # biff (11)
- # calva (5)
- # cider (4)
- # clojure (32)
- # clojure-austin (2)
- # clojure-dev (5)
- # clojure-europe (17)
- # clojure-norway (22)
- # clojurescript (23)
- # core-logic (1)
- # cryogen (1)
- # datomic (1)
- # dev-tooling (7)
- # emacs (6)
- # fulcro (63)
- # guix (1)
- # hyperfiddle (14)
- # integrant (2)
- # lsp (6)
- # missionary (4)
- # nbb (42)
- # overtone (9)
- # reitit (8)
- # specter (3)
- # sql (2)
- # squint (7)
- # tools-build (9)
Any examples using biff to serve a simple route to which you can POST a json body? I imagine I can just (get ctx :body) and call cheshire/decode on that?
the example app comes with a /api/echo endpoint that you can post stuff to: https://github.com/jacobobryant/biff/blob/5295ec19947eae0a1c71a8d9365c32ed081e3344/example/src/com/example/app.clj#L140
if you set Content-Type: application/json
on your request, it'll get parsed automatically
I will investigate further since I'm still confused about 1) This example is setting the content type on a response, not a request, which would not seem to effect how the incoming posted body is parsed. 2) The key of the function is params, which I'm not sure how that would be related to a parsed json post body. Maybe it is the parsed json post body.
Ok, your example works. You meant to set the content-type on the request in the headers created by the client. params is indeed the parsed json post body. Interestingly, this doesn't quite work if you json post an array. See below Thank you.
(defn stripe-web-hook-handler [{:keys [params]}]
{:status 200
:headers {"content-type" "application/json"}
:body params})
(comment
(client/post ""
{:content-type :json
:form-params []}))
{:cached nil,
:request-time 61,
:repeatable? false,
:protocol-version {:name "HTTP", :major 1, :minor 1},
:streaming? true,
:http-client
#object[org.apache.http.impl.client.InternalHttpClient 0x6fb6c70c "org.apache.http.impl.client.InternalHttpClient@6fb6c70c"],
:chunked? false,
:reason-phrase "OK",
:headers
{"Connection" "close",
"Content-Type" "application/json;charset=utf-8",
"Content-Length" "2",
"Server" "Jetty(10.0.7)"},
:orig-content-encoding nil,
:status 200,
:length 2,
:body "{}",
:trace-redirects []}
I would expect :body above to be "[]"
Luckily, I don't need to accept arrays now, but in other contexts I have wanted to.
interesting that a bare array doesn't work, and that I've never run into it (I have lots of endpoints expecting json); I guess I always use an object at the top level, because if I'm sending a list, I want it labeled. I suppose it can't work, because the muuntaja middleware will also parse get and posted form params into that same object. btw, you can send application/edn, and it will also be parsed into params. It's much nicer when your client is also clojure[script/dart]; you can return it too, though the client-side encoding/parsing will likely be up to you. I just have one post-edn function in my clojuredart client that prn-str's the post-body, expects edn in return, deals with cookies, etc, so my whole api on both sides is free of the kind of type-mangling you have to do with json.
hmmm yeah, if possible I would probably just set a top-level key with the array. makes it easier to add more stuff to the request/response later on if you neei to anyway, E.g. pagination info. (I wonder what that example looks like if you POST a non-empty array by the way?) if you do ever need different behavior, you can mess around with the middleware stack. new projects have it all in a middleware.clj file, though that's a relatively recent addition (couple months ago I think).