Fork me on GitHub
#babashka
<
2023-05-01
>
craftybones05:05:57

What are people using to handle form data with http-kit and babashka? Just ring middleware?

borkdude07:05:17

probably yes, or just send JSON or transit

craftybones12:05:21

Ring by itself doesn’t work. I can do JSON, but considering ring doesn’t work, there are a bunch of other things like cookies and so on. Maybe it is time to move this to Clojure

borkdude12:05:36

Can you be more specific on "ring by itself doesn't work"?

borkdude12:05:26

E.g. what did you try and what error did you get. There might be ways to fix it

craftybones12:05:36

Here’s my bb.edn(simplified)

{:paths ["src"]
 :deps {ring/ring-defaults {:mvn/version "0.3.4"}}}
And with this
user=> (require '[ring.middleware.defaults])
clojure.lang.ExceptionInfo: Unable to resolve classname: java.net.JarURLConnection [at ring/util/response.clj:278:20]

borkdude12:05:16

(ns bbform)

(require '[org.httpkit.server :as srv])
(require '[babashka.deps :as deps])

(deps/add-deps '{:deps {ring/ring {:mvn/version "1.10.0"}}})
(require '[ring.middleware.params :as params])


(defn show-params-handler [req]
  {:body (pr-str (:form-params req))})

(def handler (params/wrap-params show-params-handler))

(srv/run-server handler {:port 1234})

(println "Running server on ")
@(promise)
curl  -d foo=bar
{"foo" "bar"}%

borkdude12:05:33

with cookies:

(ns bbform)

(require '[org.httpkit.server :as srv])
(require '[babashka.deps :as deps])

(deps/add-deps '{:deps {ring/ring {:mvn/version "1.10.0"}}})
(require '[ring.middleware.params :as params])
(require '[ring.middleware.cookies :as cookies])

(defn show-params-handler [req]
  {:body (pr-str (:form-params req))})

(def handler (-> show-params-handler
                 params/wrap-params
                 cookies/wrap-cookies))

(srv/run-server handler {:port 1234})

(println "Running server on ")
@(promise)

borkdude12:05:58

As shown above, it's probably best to test individual middlewares. If a specific one doesn't work, welcome to post issues, they are likely fixable (either in bb or in ring)

craftybones16:05:51

This works for now

👍 1
pesterhazy08:05:44

Something new every month! Colocated bb.edn is a small change but reduces friction significantly

🎉 1
1
rads20:05:45

For those who miss set -x when writing Babashka scripts, I created a library to wrap babashka.process functions with a default :pre-start-fn: https://github.com/rads/xtrace

🎉 3
borkdude20:05:19

Note that the official syntax for process is now also (process opts+args) so the opts map goes first, it was "unified" with shell a while ago

rads20:05:44

Ah ok, I'll fix that

borkdude20:05:58

Note that you can get the behavior of your library as well when you do:

(alter-var-root #'process/*defaults* assoc :pre-start-fn ...)
But this dynamic var wasn't properly working until a few commits ago on master inside of bb

rads20:05:19

Good to know, thanks

rads20:05:32

I just pushed 0.0.2 with the fix for process

👍 1
pesterhazy07:05:06

I do miss set -x – thanks for starting this thread and library

borkdude07:05:40

Well this is why :pre-start-fn exists

rads13:05:40

@U04V15CAJ: I would like to encourage others to use *defaults* as well. (I made the library before I knew about *defaults*.) I suppose the only thing blocking me would be compatibility with older versions of bb. Would it be enough to bump babashka.process to use *defaults* or is upgrading to latest bb a requirement?

borkdude13:05:11

well. bb process is built-in to bb so either upgrade bb (but first it has to be released, the commit is still only on master) or require process with :reload . :reload will work in older versions

borkdude13:05:49

*defaults* has been there for a long time, but it wasn't properly exposed in SCI, but when you load bb process from source it will work with current versions

rads14:05:48

All right. It sounds like there is still some value in this library in the short term. I can add a note about using *defaults* in the README for those using the newest bb and deprecate the library later

👍 2