This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2022-06-30
Channels
- # announcements (40)
- # babashka (41)
- # beginners (32)
- # calva (15)
- # clara (8)
- # clj-kondo (14)
- # cljs-dev (30)
- # clojure (37)
- # clojure-dev (8)
- # clojure-europe (21)
- # clojure-norway (21)
- # clojure-uk (4)
- # clojured (3)
- # clojurescript (4)
- # community-development (10)
- # core-async (13)
- # cursive (23)
- # datomic (15)
- # emacs (9)
- # fulcro (3)
- # google-cloud (4)
- # graphql (24)
- # gratitude (2)
- # holy-lambda (4)
- # honeysql (5)
- # hyperfiddle (9)
- # keechma (1)
- # klipse (5)
- # lsp (23)
- # malli (4)
- # missionary (32)
- # pathom (28)
- # re-frame (2)
- # reagent (40)
- # reitit (17)
- # releases (2)
- # remote-jobs (1)
- # shadow-cljs (25)
- # specter (3)
- # vim (19)
- # xtdb (41)
I've just started looking at https://github.com/babashka/pod-babashka-aws/ and I'm using it with holy-lambda in an aws lambda and I'm very unclear how I'm supposed to do a http get / post? There does seem to be a http-client
inside the pod's source, but when I try and require it, it's not there. How exactly am I supposed to make http requests to other services etc?
You can also try https://github.com/grzm/awyeah-api which works from source
Sorry, I suppose the question is a bit wider, where is the http client in babashka then? Is it inside the client? I've seen references to curl, but I'm not sure I should be calling on curl in a lambda context?
I'm using it for the first time to do some lambda development, so I'm still unfamiliar with the environment 😃
@U0JUM502E Ah right. For http you have the following options:
- babashka.curl
- org.httpkit.client
- java.net.http
or a wrapper for java.net.http like https://github.com/schmee/java-http-cljCalling curl from a lambda context, not sure if it works, but you have the other options as well
So for example, I just add:
java-http-clj/java-http-clj {:mvn/version "0.4.3"}
in my deps.edn, and then I should be able to require it at my repl?So what's the relationship between them, there are some examples where they suggest putting it in deps.edn, are they independent?
I don't know if they're ones you control or if it's just using a different paradigm, for example this was super confusing. I assume it works some other way... https://github.com/FieryCod/holy-lambda/tree/master/examples/bb/native/cognitect.aws.api
Ok, that seems to be working 😃... Not sure if it's going to break when it comes time to deploy it, which is what I'll test next! Thanks for your help @U04V15CAJ 😃...
@U0JUM502E this is example of using cognitect AWS API with native backend whereas you’re using babashka one ;D
@U04V15CAJ, just trying out clj-http
instead and I'm getting this:
(require '[clj-http.client :as client])
: Unable to resolve classname: org.apache.commons.codec.binary.Base64 humble-aws-fn.core
I've added imports, but it seems to not be picking it up?
Basically I need form handling for the http client, so I can send forms, which java-http-clj
doesn't appear to cover?
Sure, for example:
(http/post ""
{:headers {"ContentType" "application/json"}
:body {"id" 1234567 "data" {"a" "a"}}})
Take this, what I have to work with is :body
, so if I want to send a raw json body, that's fine, I just call cheshire.core/generate-string
on the value at :body
, but to send form encoded? That's a bunch more complex. So normally this falls under stuff I expect the http client lib to handle, but in this case it doesn't, so I'm just wondering is there a more fully featured client, for example clj-http
https://github.com/dakrone/clj-http@U0JUM502E Ah right. I think for this to work you just have to do something like this:
(require '[clojure.string :as str])
(defn encode [v] (java.net.URLEncoder/encode v))
(defn encode-map [m] (str/join "&" (reduce-kv (fn [acc k v] (conj acc (str k "=" (encode v)))) [] m)) )
(encode-map {"a" "1" "b" "foobar$123"}) ;;=> "a=1&b=foobar%24123"
But I think it's reasonable if java-http-clj would do this for you with the given content type. What do you think @U3L6TFEJF?
Hmm, ok, that should work I think but there's the server isn't accepting it, so there's some gap. I'm going to try and work out what...
I'll be back in an hour after dinner. For hato you need to add babashka spec alpha to your bb edn I think too
I didn't realise the form encoding step could be done that easily, I remember having to setup some object and do some setting etc when I was in jsland. Giving hato a look
Hmm, not sure how to use it, after I added it to bb.edn
.
When required it I got Could not find namespace: clojure.spec.alpha
adding clojure.spec.alpha to bb.edn
and deps.edn
didn't seem to help.
EDIT: Ahh, just spotted the message about a special version of spec alpha
@U0JUM502E Add this to your bb.edn:
org.babashka/spec.alpha {:git/url ""
:git/sha "1a841c4cc1d4f6dab7505a98ed2d532dd9d56b78"}
You can also try the built-in client org.httpkit.client
:
(require '[org.httpkit.client :as client])
@(client/post "" {:form-params {"a" "foo bar"}})
Yeah, it's https://github.com/http-kit/http-kit :) So we have three options in bb: • bb.curl • httpkit client • java.net.http
And even a fourth one: HttpUrlConnection and clj-http-lite which is based on that (external library)
Really liking Babashka's synergy with Bash and Unix. Long lines, best viewed with wide slack.
A PR to make bb compatible with #malli https://github.com/metosin/malli/pull/718 Instructions on how to try it now are in the PR!