day8 / re-frame-http-fx

A re-frame "effects handler" for performing Ajax tasks (via cljs-ajax)
MIT License
259 stars 27 forks source link

How do I attach parameters to POST requests? #6

Closed freckletonj closed 7 years ago

freckletonj commented 7 years ago

Resources I looked to for answers: https://github.com/Day8/re-frame-http-fx https://github.com/Day8/re-frame-http-fx/blob/master/src/day8/re_frame/http_fx.cljs https://github.com/Day8/re-frame/blob/master/docs/Talking-To-Servers.md

I mention that as a place where an explanation might be appreciated by fellow devs.

My understanding is that this capacity doesn't exist yet, so I may just need to roll my own, and I'm willing to contribute a PR if you like.

danielcompton commented 7 years ago

Hey

This library is built on cljs-ajax, and relies mostly on the syntax of ajax-call. I think [:params](https://github.com/JulianBirch/cljs-ajax#ajax-request) is what you need to provide and it'll be passed through. Definitely happy to take a PR on docs for this (or capability if required).

freckletonj commented 7 years ago

@danielcompton I like this idea, and that's what I would have expected (to be able to send through arbitrary parameters), but... still can't figure out if it's allowed.

I've been over the code in http_fx.cljs a few times now, and I don't think having a :params parameter conforms to the current spec, nor could it make it through either of the two helper functions since, it seems by looking, that on-success, and on-failure are the only parameters hard-coded to be passed around.

Am I reading it wrong?


EDIT: Added simple solution below

I just created something simple that works, just allows me to pass in arbitrary options like ajax expects:

(def ajax-methods {:post POST
                   :get  GET})
(reg-fx
 :http
 (fn [{:keys [method uri options
              on-success on-failure]}] ; options - as expected by ajax calls
   (let [m-fn (method ajax-methods)]
     (m-fn uri (-> options
                   (assoc :handler       #(dispatch (conj on-success %))
                          :error-handler #(dispatch (conj on-failure %))))))))

this avoids the need for importing the google libraries, which, i'm not sure what they afford this library above and beyond what ajax does on its own. I'd be curious if you had time to mention it :)

danielcompton commented 7 years ago

I've been over the code in http_fx.cljs a few times now, and I don't think having a :params parameter conforms to the current spec

The current spec only includes required keys, but doesn't have specs for optional keys yet. All of the keys in an fx map are passed through to ajax-request, except for :handler and :api. We assoc and dissoc keys, but most of the map stays untouched. In general the API for this library should be identical to ajax-request apart from those minor differences.

I've added some more tests and an example in the README which show how to do POST requests with this library.

I just created something simple that works

Looks cool! I'd recommend publishing this as an fx handler library, as I can imagine some people could prefer the easier API provided by GET and POST.

this avoids the need for importing the google libraries, which, i'm not sure what they afford this library above and beyond what ajax does on its own.

Not quite following here? We don't import goog.net.XhrIo, but it's used by cljs-ajax.

Thanks for the good question, we needed to add some docs for this 😄

cmal commented 7 years ago

Is it possible to send a request of :format application:x-www-form-urlencoded? There is only an example of the formData. I've tried several methods but still not find a way out.

danielcompton commented 7 years ago

Not sure sorry, but if you can do it with cljs-ajax, you can do it with this lib.

cmal commented 7 years ago

Thanks, I'll ask that in cljs-ajax.