This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2017-03-19
Channels
- # arachne (1)
- # beginners (108)
- # boot (50)
- # cljs-dev (7)
- # clojure (76)
- # clojure-ireland (1)
- # clojure-romania (1)
- # clojure-russia (7)
- # clojure-spec (33)
- # clojure-taiwan (1)
- # clojure-uk (36)
- # clojurescript (46)
- # core-async (13)
- # datomic (146)
- # defnpodcast (1)
- # editors (1)
- # garden (2)
- # hoplon (1)
- # jobs (1)
- # lumo (7)
- # off-topic (21)
- # om (9)
- # pedestal (1)
- # re-frame (25)
- # reagent (5)
- # specter (2)
- # testing (3)
- # unrepl (3)
- # untangled (9)
- # vim (1)
@pcbalodi tnx!! i still need to learn a lot them i have no idea how you came up with destructuring thing although i saw it in use.
anyone available to rubber duck a POST request problem? the problem is more with the request itself than anything clojure-specific, i think. any clues are much appreciated! i’m using the reagant-template with cljs-ajax, ring/compojure. this is the request on the front end:
(POST "/list"
{:params text
:handler (fn [data]
(reset! state-atom (cljs.reader/read-string data)))
:error-handler error-handler})))
what’s happening is that i’m making a post request, sending the params (which is a string or an object or whatever data) and then the handler gets the response back or the error handler fires.
the handler works! i get a “successful” response with the updated data. the backend functions are working properly in the repl with test data, but the params don’t ever seem to get over to the backend the right way, they never get added to the new data. GET requests are working as expected.
expected response: {0 {:text "first response" :complete true :id 0} 1 {:text "another response" :complete true :id 1}}
but what i get back is {0 { :complete true :id 0} 1 {:complete true :id 1}}
the :text
part is what is sent through the params, but doesn’t seem to get to the backend.what are you doing to access the post data?
i made some progress since that post. was able to log out the request on the backend (which is just the handler functions and an atom as a “database”).
so, i think the first thing is figuring out what the content type is supposed to be. the params are definitely empty as well.
for a post request, it should be in :form-params, though the wrap-params
middleware will also merge it into the :params
for most ring setups, you can default to form-encoded (and be sure to form encode your params) or you can set it to application/json and send a json string
are you sure cljs-axaj doesn't want the params under :body
?
usually for post requests the payload would be in the body
n/m - I found it in the docs, it accepts either
@jennifer one thing that I've found helps when getting things set up, is to start with a known quantity on one side of the request - so either use the clj backend plus a curl request that is known to work on another server, or the cljs frontend, sending the request to a different server (one where you can see the data it gets, of course).
i did try using body as well, which seems to log out a Java object. i can post my current request if that might help.
what happens if you try to do (slurp (:body request))
somewhere where you can access the request
for example the handler
the thing that gets me about that request, is it has a :content-length 21
but I don't see the decoded request body anywhere, which makes me think that if you slurped the body you might see a raw request that was 21 bytes long
so i should be trying to do the slurp on the backend in the POST router? or the clojurescript/frontend handler? i apologize for the super newbie questions. i’ve mostly been a front end js dev and am new to clojure/backend.
how did you print that request?
that's a back end ring request object, anywhere in the clojure back end code where you can access that object you should be able to access the :body key and attempt to slurp it. For example, it looks like that request was sent to /list
so you could add a line to the code of /list
that slurps the body and prints it
OK I find it really weird on line 3 that (request)
is in parens
but anyway, you could add (println (slurp (:body params)))
list-add is written as if it was getting the params of the request as sent by the client, but it's being given the full request
(but after we figure out how to get the actual params we can sort that out too)
yup. the original route was (list-add (:params request))
sorry. i’ve had to backtrack a couple times to post snippets. 🙂
I assume you started passing in the full request for debugging purposes
right!
so yeah, that println / slurp / body might be informative
i stuck a (println “yo”)
before it, just to make sure something was working. that logs properly, but i get nothing from the body.
OK - so something is consuming the body but you aren't seeing the data in the request
that sounds about right and matches what i’m seeing returned to the front end handler function.
well, the front end handler function gets whatever /list returns
you just happen to be returning a hash map with none of the keys that ring likes at the moment, which translates to an empty reply
(swap! returns the new state of the object you swapped on)
am i correct in thinking that this is centered on how i’m sending data from the front end?
are you explicitly using the wrap-params
middleware?
@jennifer - no, the first problem is that the back end isn't seeing any of the data the front end is actually sending
it can't return a meaningful result until it knows the content of hte request
try commenting out line 12
I think wrap-defaults already wants to get your params, if you do it twice you could end up with nothing
in the future you might want to keep line 12, and replace wrap-defaults, but that's trickier since wrap-defaults does a few things
I think if you comment the wrap-restful-format and restart your http server, you might see something useful in the request body in the list handler (or even the form-params key in the request)
after a full restart?
confirmed. still 404ing. it wasn’t doing that before i put line 12 in, so maybe i’m missing something else.
are you using version control?
yup. using git. i haven’t committed in a bit because i was trying to get this working first.
looking at your diff might be helpful at this point - and if you get to another stable point, that might be a good starting point for a debugging branch
yeah, my last stable point doesn’t have this working either. i have GET requests working fine (yay!) i’m just hung up on POST. is it possible i’m missing something with middleware or dependencies or something? i had started with reagent-template which did most of the setup for me.
right, but your last stable build point doesn't 404
correct. so i got back there easily enough. there’s only a couple functions i’m dealing with, so i hadn’t mucked things up too badly. 🙂
one of my usual resources came on line. she might be able to give me some more insights. fingers crossed
Hi all, I'm working on 4clojure and trying to do the Fibonacci sequence task. I've got the following code...
When I run it, I get
IllegalArgumentException Don't know how to create ISeq from: cbat.core$eval1588$fn__1589 clojure.lang.RT.seqFrom (RT.java:542)
@danp you can't take from a function
perhaps you meant to wrap an extra set of parens so the function would be called?
or you could remove the fn part altogether, the loop can stand alone
the next issue is the loop is not lazy, so the take can't prevent it from being infinite
it can't return its collection, because there's no codepath that can ever return the collection
because it is looping infinitely
there's no codepath that ever returns the sequence
you can use lazy-seq
, then take will help
otherwise you can check the count of 10 in your loop, with a conditional to return the coll
This was something I couldn't quite get my head around - do i just put fib
before the recur?
you have two options - make a proper lazy-seq, or have a conditional to return fib
the first is a more useful thing genarally in clojure, the latter is easier right now
it's up to you
I was just going to say the latter sounds like I could do it, the former I can't quite think how to do right now 🙂
in that case, have an if, check the size, one branch of the if calls recur, the other returns fib
if you want a more efficient version of the function, you would want to keep track of the last two items instead of reversing the list or taking the last item out, which is half of the work needed to do the lazy-seq solution properly
I did wonder how well the reverse would perform with larger sequences - I'll keep that in mind, thanks very much.
last on lists is O(n), as is reverse, and you can avoid both if you keep the last two items in an extra loop binding
... I think
which makes your fib O(n!) or something
The loop counter version worked fairly easily, cheers @noisesmith !
yeah, the other stuff is extra credit - you should see it in other people's solutions
@noisesmith thanks so much for your help before! there were some configuration things set up incorrectly and i’ve got things working now. 😄
excellent
Hey @noisesmith, I've got the lazy version working now. Admittedly, I stumbled on a Fibonacci-specific example when I was looking at: https://clojuredocs.org/clojure.core/lazy-seq
heh, it is the classic after all
But I managed to do it without referencing back to the example. Just wondering though, how comes this works?
a lazy-seq acts like a sequence, but the rest of the sequence is actually a function
concat only accepts collections
l1 is not a collection
and once you have to create the collection, lazy-seq is a more natural answer in comparison (but [l1] as the first arg to concat should work too)
odd, somehow not lazy enough I guess
Think I'll call it a night now anyway and pick up 4clojure in the morning. Thanks again for your help
:thumbsup: