Clojure Noir Json Put

发布于 2024-12-25 13:43:30 字数 560 浏览 2 评论 0原文

我正在完成教程 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

梦里南柯 2025-01-01 13:43:30

如果您在帖子中传递“attrs=somevalue”,那么这将起作用,但如果您试图捕获所有键值对,这对我来说在 1.3.0-beta1 中有效:

(defpage [:put "/elems/:id"] attrs 
  (response/json {:attrs attrs}))

然后:

$ curl -H "Accept: application/json" -X PUT -d "foo=bar" http://localhost:8080/elems/123
=> {"attrs":{"id":"123","foo":"bar"}}

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:

(defpage [:put "/elems/:id"] attrs 
  (response/json {:attrs attrs}))

then:

$ curl -H "Accept: application/json" -X PUT -d "foo=bar" http://localhost:8080/elems/123
=> {"attrs":{"id":"123","foo":"bar"}}
策马西风 2025-01-01 13:43:30

使用此处中的 Chris Granger 的 JSON 解析中间件函数,并按照描述使用它 此处接收 JSON 数据。

在你的情况下,它看起来像

(defpage [:put "/elems/:id"] {{:keys [attr1 attr2 attr3]} :backbone }
         "OK")

但你只需要首先添加“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

(defpage [:put "/elems/:id"] {{:keys [attr1 attr2 attr3]} :backbone }
         "OK")

But you just need to add that "backbone" (I personally renamed it to "json-params") middleware first.

焚却相思 2025-01-01 13:43:30

那么这些是表单参数吗?如果是这样,像您在这里所做的那样解构参数应该可以正常工作。您可以使用 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文