How to get repeating request parameters in Compojure

泄露秘密 提交于 2019-12-10 03:59:43

问题


I can get to the request parameters easily with:

(:foo params)

However, when I have a request like this:

/api?foo=1&foo=2&foo=3

I only get back "3" while I would expect an array ["1","2","3"].

I'm not sure why this is happening because when I look at the code in:

https://github.com/ring-clojure/ring-codec/blob/master/src/ring/util/codec.clj#L128

It seems to call assoc-conj which is supposed to turn multiple params of the same name into a vector containing the values.

Am I missing something here or is this a bug?


回答1:


use a standard Clojure destructuring form:

(GET "/api" {{:strs [foo]} :query-params} (str foo))

curl "http://localhost:3000/api?foo=1&foo=2&foo=3" 
==> ["1" "2" "3"]

doc: https://github.com/weavejester/compojure/wiki/Destructuring-Syntax




回答2:


I've never encountered this issue myself, but if it is indeed impossible to get a hold of the multiple values without manually parsing the URL yourself, it sounds like it's worth filing a bug report over at https://github.com/weavejester/compojure/. (Unless it's a feature.. There are some other things in Compojure and Clout that's a bit non-standard but highly convenient.)



来源:https://stackoverflow.com/questions/15635518/how-to-get-repeating-request-parameters-in-compojure

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!