Accessing posted json with ring format-params middleware

空扰寡人 提交于 2019-12-10 23:31:10

问题


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.

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)))

It just returns this: {}

What am I doing wrong?


回答1:


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



来源:https://stackoverflow.com/questions/13150297/accessing-posted-json-with-ring-format-params-middleware

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!