首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何简单地编写一个拦截器,使用Pedestal从get/post请求中提取json

如何简单地编写一个拦截器,使用Pedestal从get/post请求中提取json
EN

Stack Overflow用户
提问于 2021-10-19 07:01:02
回答 1查看 63关注 0票数 1

我正在做一个简单的API,它需要从json/edn请求中读取主体参数。我试图让程序将内容作为edn对象进行回显,但我的路由似乎不起作用。

代码语言:javascript
复制
(def routes
  (route/expand-routes
   #{["/echo" :get [body-params/body-params print-response]  :route-name :greet]}))

拦截器

代码语言:javascript
复制
(def print-response {:name ::print-response
                     :leave (fn [context]
                              (let [content-type (get-in context [:request :content-type])
                                    updated-response (assoc (-> context :response) :headers {"Content-Type" content-type}
                                                            :status  200
                                                            :body (get-in context [:request] :edn-params))]
                                (assoc context :response updated-response))
                              )
                     } 
                              
                             
)
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-10-19 07:48:26

fix :在body-params周围加上parantheses解释body-params实际上body-params是一个创建拦截器的高阶函数,因此必须调用它

代码语言:javascript
复制
(def routes
  (route/expand-routes
   #{["/echo" :get [(body-params/body-params) print-response]  :route-name :greet]}))
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69626331

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档