Fork me on GitHub
#beginners
<
2019-08-04
>
Ahmed Hassan08:08:05

how can i clj-http.client/post with :body-params?

Ahmed Hassan08:08:39

because docs only mention :form-params and :query-params.

little-dude08:08:23

my understanding is that :form-params is basically :body-params

Ahmed Hassan08:08:29

When I print request on server it includes :form-params under :params and :form-params keys.

Ahmed Hassan08:08:30

I need params under :body-params key.

Ahmed Hassan08:08:33

Basically I'm using day8.re-frame.http-fx which sends :post params under :body-params key. So, all handlers at server extract :body-params from request.

Ahmed Hassan08:08:16

I want to do similar :post requests with clj-http.client/post, to inspect routes from REPL.

valtteri11:08:23

@ahmed1hsn I usually don't use a HTTP-client to test routes from REPL. If your backend is Ring compatible (which it probably is) you can call the handler function directly and pass a request map created with https://github.com/ring-clojure/ring-mock

👍 4
Ahmed Hassan11:08:05

handlers are wrapped anonymous functions inside integrant init-key multimethod.

Ahmed Hassan13:08:57

I don't know how can I find reference to handler to run ring-mock

valtteri14:08:52

How are you defining your handlers? Which library are you using?

Ahmed Hassan14:08:39

I'm using duct with Ataraxy as routing lib.

valtteri14:08:36

Ok I don’t have experience with them so it’s hard to help without seeing the code.

valtteri11:08:13

But if you want to use clj-http you should probably put the body under :body, as in the first example at https://github.com/dakrone/clj-http#post

👍 4
Ahmed Hassan12:08:10

Thanks it works, but I have to escape double quotes with slashes.

valtteri13:08:02

Yes, body is expecting a string. You probably want to use a library to convert whatever data structure you have to string. For example cheshire or jsonista if you're using JSON for communication. If you're using EDN you can just use pr-str.

👍 4
valtteri13:08:29

user> (pr-str {:a 2 :b "kissa"})
"{:a 2, :b \"kissa\"}"

👍 4
bartuka14:08:57

is it useful to use pmap in the middle of a ->> or -> processing pipelines? I could not create any example where the benefits of parallelization was evident. Ex:

(->> data
       (map fn-transf1)
       (pmap fn-transf2)
       (filter fn-business1))

Alex Miller (Clojure team)15:08:16

pmap is useful in a pretty narrow set of circumstances. The map task (fn-transf2) really needs to be pretty coarse grained to exceed the parallelization overhead

bartuka19:08:11

thanks @U064X3EF3 I thought so. I faced situations where I needed to perform ETL to several clients. We implemented the code to handle 1 client and used pmap to run for all of them later. However, even in this scenario, I'm feeling that I would like more control about the processes that are running. For example, the number of threads, backpressure and so on. Is this a valid concern? Is pmap being applied in the a proper scenario?

Alex Miller (Clojure team)20:08:58

pmap is good for a quick hack. For anything serious, I usually don’t want the laziness that pmap includes and I just a Java executor to get more control or a lib like claypoole

👍 4
Chase21:08:23

Hello! I would love a critique on this little temperature converter program if willing. https://pastebin.com/xpkCkmhU My concerns/questions: 1) Almost all my functions have side effects, right? If a function calls another function that has side effects is that now considered also impure? How do I make this more pure? 2) How would you approach error handling? If the input isn't perfect this thing won't work. 3) How to approach looping this so you can keep inputting temps until you write "quit"? Every attempt I make hasn't worked right, I'm not sure if I'm understanding the underlying mechanics behind flush and read-line maybe. 4) I also need to handle negative temps so would like to see how you would take and parse user input that makes this all a bit better. Thank you!

schmee21:08:05

@chase-lambert I made some changes to the code with some comments: https://gist.github.com/schmee/b6ce2ec9b49733696fd99ffecc413df1, I hope it’s useful 🙂

Chase21:08:36

This is perfect! Exactly what I was looking for. Thank you! Already, just using str instead of println to make the parsing pure is such a "d'oh! of course" moment

👍 4