使用ring format-params中间件访问已发布的json [英] Accessing posted json with ring format-params middleware
问题描述
我想在clojure中使用一个非常简单的API。我使用rack.middleware.format-params中间件将输出转换为json,并将输入从json转换为clojure数据结构。
I'm trying to make a very simple API using ring in clojure. I'm using the rack.middleware.format-params middleware to convert the output to json, and the input from json to clojure data structures.
我有输出工作很好,但我不能为我的生活访问通过json发送的参数。下面是一些适用于get请求的代码,但是我不能获取POST请求来返回它接收到的json
I've got the output working nicely, but I can't for the life of me access the parameters sent through json. Here's some code that works for get requests, but I can't get the POST request to return the json it recieves
(ns testing.core
(:use [compojure.core]
[ring.middleware.format-params :only [wrap-json-params]]
[ring.middleware.format-response :only [wrap-json-response]]
[ring.adapter.jetty])
(:require [compojure.handler :as handler]))
(defroutes app-routes
(GET "/"
[]
{:body {:hello "world"}})
(POST "/"
{params :params}
{:body params}))
(def app
(-> (handler/api app-routes)
(wrap-json-params)
(wrap-json-response)))
{}
我做错了什么?
推荐答案
我是一个白痴,意识到,我没有发送json Content-Type头。希望没有人会犯同样的错误:P
I'm an idiot and realised that I wasn't sending the json Content-Type header. Hopefully no-one else makes the same silly mistake :P
这篇关于使用ring format-params中间件访问已发布的json的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!