Fork me on GitHub
#beginners
<
2020-03-13
>
MorongÖa06:03:30

Has anyone ever used clojurescript with storybook?

gibb15:03:41

[SOLVED] nevermind I googled how to do it in Java again, and actually used the correct method now! I ended up using

(:import (org.apache.commons.codec.binary Base64))

(c/get url {:headers {"Authorization"
                      (str "Basic " (Base64/encodeBase64String (.getBytes (str email ":" token))))}})
Original: How do I do this in clojure : echo -n :topsecreted12341234agfawerf | base64

ghadi15:03:04

post your solution @gbson ^ so that others may benefit?

borkdude16:03:39

@gbson If you need to do this from the command line for whatever reason, bb also has java.util.Base64:

$ echo -n :topsecreted12341234agfawerf | bb '(String. (.encode (java.util.Base64/getEncoder) (.getBytes (read-line))))'
"bm90bXllbWFpbEBnbWFpbC5jb206dG9wc2VjcmV0ZWQxMjM0MTIzNGFnZmF3ZXJm"

borkdude16:03:59

although it's more characters than base64 🙂

gibb17:03:42

@ghadi good point I should have done that from the beginning 🙂

gibb17:03:57

The first library I tried using was java.util.Base64 but then I ran into some classpath bugs possibly with IDEA / Cursive which made the whole JDK invisible to Cursive (it still worked tho). Since the IDE had the apache commons libs in sight I misused them and then used them correctly 😄

gibb17:03:43

Overall I'd rate the experience a 4/10 compared to piping to base64 xD

ghadi17:03:09

sounds like you have a lot going on 🙂

echo "bytes to encode" | clojure -e '(-> (java.util.Base64/getEncoder) (.encodeToString (.getBytes (slurp *in*))) println)'
Ynl0ZXMgdG8gZW5jb2RlCg==
echo "bytes to encode" | base64
Ynl0ZXMgdG8gZW5jb2RlCg==

gibb17:03:42

Hehe that's why I'm in the beginners channel 😛

ghadi17:03:52

🙂 I wonder if there is something cursive related that messed up your classpath? I'd debug that before making workarounds your code

gibb17:03:24

Yeah it seems to be I reported it there, I could work around it by switching the JDK to v1.8 and then it found everything

Luis C. Arbildo18:03:22

Hi guys, I wondering what are these thing "MongoOptions" "SeverAddress" why have this "^" and why can be before the name of a variable?

Frank Jay21:03:05

And also user types: (eval-not '(not (or x y))) and gets -> (and (not x) (not y))

Frank Jay21:03:26

How would i go about doing this? Any help would be appreciated. Ps. I really like Clojure

phronmophobic21:03:25

there’s a couple different approaches, but just to provide an example

(defn eval-not [input]
  (let [[-not [-and x y]] input
        new-form `(or (not ~x) (not ~y))]
    new-form))

Frank Jay21:03:00

okay, interesting. is there a way you could do it with the code i already provided? (the cond example)

noisesmith21:03:01

that ` form is going to create a bunch of namespaced symbols in the output

phronmophobic21:03:12

here’s an example that avoids the backtick

(defn eval-not [input]
  (let [[-not [-and x y]] input
        new-form (list 'or (list 'not x) (list 'not y))]
    new-form))

phronmophobic21:03:10

if you’re rewriting expressions, then the backtick, ` , can be very helpful since that’s what its purpose is*

dev.4openID22:03:43

when I use this on a cli curl -X GET '"https://reqres.in/api/users?page=2' -H 'API-Key:MySecret' I get the correct json results in clojure snippet (client/get "https://reqres.in/api/users?page=2" {:headers ["API-Key" "MySecret"]}) it fails to clj-http: status 401 what am I doing wrong? I have been all over the clj-http github docs but ....

seancorfield22:03:51

{:headers {"API-Key" "MySecret"}} -- it's a hash map, not a vector.

phronmophobic22:03:39

one tactic to note the difference is to use netcat, nc

phronmophobic22:03:17

eg. nc -l 1234 then, curl -X GET '"' -H 'API-Key:MySecret'

phronmophobic22:03:43

followed by nc -l 1234 (client/get "" {:headers ["API-Key" "MySecret"]})

phronmophobic22:03:15

that may help you pinpoint exactly what is the difference in payloads

seancorfield22:03:56

@smith.adriane I'm not sure what you're suggesting -- nc 1234 is not a valid command on macOS:

nc: missing hostname and port

noisesmith22:03:34

also [] for :headers is not valid

seancorfield22:03:42

I said that above ^

seancorfield22:03:55

{:headers {"API-Key" "MySecret"}} -- it's a hash map, not a vector.

dev.4openID22:03:57

thanks @seahckr

seancorfield22:03:11

@smith.adriane In this case nc doesn't help because the client/get call (in clj-http) fails before it even sends the request (because the :headers argument was wrong).

seancorfield22:03:45

But it seems like a neat trick for requests that do get through. How would you send back a valid response to the client tho?

phronmophobic22:03:07

cat valid-response | nc -l 1234

seancorfield22:03:45

Right, but you'd need to know how to write a valid HTTP response -- that's what I'm asking you to elaborate, so that other beginners here can use that trick for testing.

phronmophobic22:03:23

I’m actually not sure how to send back an* *in*valid response

phronmophobic22:03:33

it depends on what the client is expecting

seancorfield22:03:14

echo "HTTP/1.1 200 OK" | nc -l 1234
That will send back a success status and an empty response which might be enough for testing.

👍 1
phronmophobic22:03:28

typically, for this type of debugging, I don’t worry about generating a valid response. it’s mostly for comparing requests to figure out* exactly how they differ

jsn22:03:33

you can probably use something like ncat -l <port> -c "tee log | ncat 80" or something (but at this point just using tcpdump seems easier)

dev.4openID22:03:53

looks like I will have to dive in with wireshark to see the differences?

dev.4openID22:03:22

or maybe use mitmproxy? bit of a fiddle around?