hey all
i'm trying to convert a clj map to x-www-form-urlencoded for stripe api
(form-encode {:success_url ""
:line_items [{:price_data {:currency "USD"
:product_data {:name "product 1"}}
:quantity 1}]
:mode "payment"})
; => "success_url=&line_items=price_data=currency=USD&product_data=name=product+1&quantity=1&mode=payment"
expected (a bit of mismatch on the values):
curl \
-u "***:" \
--data-urlencode success_url="" \
-d "line_items[0][price]"=price_1MotwRLkdIwHu7ixYcPLm5uZ \
-d "line_items[0][quantity]"=2 \
-d mode=payment
it ignores the vector
do you know of any encoding that can transform a clojure map into this Stripe api?Form encoding doesn't have a canonical nested format, Stripe is just using a convention. You'll need to convert your nested map into a flat map first, then encode it. I don't know of any library that does this, but there may be one out there.
Here's a quick function I put together that flattens the data structure using the above convention:
(defn flatten-nested [m]
(letfn [(keys->str [[k1 & ks]]
(apply str k1 (for [k ks] (str "[" k "]"))))
(assoc-nested [ks]
(fn [m k v]
(let [ks (conj ks (if (keyword? k) (name k) (str k)))]
(if (coll? v)
(reduce-kv (assoc-nested ks) m v)
(assoc m (keys->str ks) v)))))]
(reduce-kv (assoc-nested []) {} m)))thanks! i have found that http-kit has a built in for that (a pretty new one) https://clojurians.slack.com/archives/C01ALH5JGRJ/p1733843309270729