如何在Compojure中获取重复的请求参数 [英] How to get repeating request parameters in Compojure

查看:93
本文介绍了如何在Compojure中获取重复的请求参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以很容易地得到请求参数:

 (:foo params)



但是,当我有这样的请求时:

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

回到3,而我希望数组 [1,2,3]



我不知道为什么会发生这种情况,因为当我看下面的代码:



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



似乎是调用 assoc-conj 它应该把同名的多个params变成一个包含值的向量。



我缺少这里的东西, ?

解决方案

使用标准的Clojure解构形式:

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

curlhttp:// localhost:3000 / api ?foo = 1& foo = 2& foo = 3
==> [123]

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


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?

解决方案

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

这篇关于如何在Compojure中获取重复的请求参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆