Clojure Noir Json Put
我正在完成教程 Mark McGranaghan REST 教程,但是我我正在尝试使用 Noir 来代替。
我可以添加新项目,但它永远不会占用 PUT 命令的主体。
我认为问题在于我如何构建 put 语句。我认为 {:keys [id attrs]} 是问题所在,因为我试图告诉它 json 内容在 url 中,如果不是,则在正文中。谁能告诉我如何使用 noirs defpage 从身体中检索它?
(放在单独的 elem 文件中)
(defn put [id attrs]
(let [new-attrs (merge (get id) attrs)]
(swap! elems assoc id new-attrs)
new-attrs))
(defpage [:put "/elems/:id"] {:keys [id attrs]}
(json-response (elem/put id attrs)))
I'm working through the tutorial Mark McGranaghan REST Tutorial however I'm trying to do it using Noir instead.
I can add new items, however it never takes the body of the PUT command.
I think the problem with how I'm trying to construct the put statement. I'm thinking the {:keys [id attrs]} is the issue, because I'm trying to tell it the json content is in the url, when its not, its in the body. Can anyone advise how i retrieve it from the body using noirs defpage?
(put is in a separate elem file)
(defn put [id attrs]
(let [new-attrs (merge (get id) attrs)]
(swap! elems assoc id new-attrs)
new-attrs))
(defpage [:put "/elems/:id"] {:keys [id attrs]}
(json-response (elem/put id attrs)))
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您在帖子中传递“attrs=somevalue”,那么这将起作用,但如果您试图捕获所有键值对,这对我来说在 1.3.0-beta1 中有效:
然后:
If you are passing "attrs=somevalue" in the post then that will work, but if you are trying to capture all the key-value pairs, this works for me in 1.3.0-beta1:
then:
使用此处中的 Chris Granger 的 JSON 解析中间件函数,并按照描述使用它 此处接收 JSON 数据。
在你的情况下,它看起来像
但你只需要首先添加“backbone”(我个人将其重命名为“json-params”)中间件。
Use Chris Granger's JSON-parsing middleware function from here, and use it as described here to receive JSON data.
in your case it'll look like
But you just need to add that "backbone" (I personally renamed it to "json-params") middleware first.
那么这些是表单参数吗?如果是这样,像您在这里所做的那样解构参数应该可以正常工作。您可以使用 noir.request 获取 defpage 内的整个请求。我会看一下它,看看它包含什么。它应该能够显着地澄清事情。
So these are form parameters? If so, destructuring params like you did here should work just fine. You can get the whole request inside the defpage using
noir.request
. I'd take at look at that and see what it contains. It should clarify things significantly.