我正在玩compojure-api,并且在尝试为我的简单webapp管理Content-Type时被阻止。我想要的是发出一个纯文本的HTTP响应,但是Compojure-API一直将它设置为"application/json“。
(POST "/echo" []
:new-relic-name "/v1/echo"
:summary "info log the input message and echo it back"
:description nil
:return String
:form-params [message :- String]
(log/infof "/v1/echo message: %s" message)
(let [resp (-> (resp/response message)
(resp/status 200)
(resp/header "Content-Type" "text/plain"))]
(log/infof "response is %s" resp)
resp))但是curl显示服务器响应Content-Type:application/json。
$ curl -X POST -i --header 'Content-Type: application/x-www-form-urlencoded' -d 'message=frickin compojure-api' 'http://localhost:8080/v1/echo'
HTTP/1.1 200 OK
Date: Fri, 13 Jan 2017 02:04:47 GMT
Content-Type: application/json; charset=utf-8
x-http-request-id: 669dee08-0c92-4fb4-867f-67ff08d7b72f
x-http-caller-id: UNKNOWN_CALLER
Content-Length: 23
Server: Jetty(9.2.10.v20150310)我的日志显示函数请求的是“纯文本”,但不知何故框架胜过了它。
2017-01-12 18:04:47,581 INFO [qtp789647098-46]kthxbye.v1.api [669dee08-0c92-4fb4-867f-67ff08d7b72f] - response is {:status 200, :headers {"Content-Type" "text/plain"}, :body "frickin compojure-api"}如何在Compojure-API Ring应用程序中获得对Content-Type的控制?
发布于 2017-01-13 15:25:03
compojure-api以Accept客户端请求的格式提供响应,该格式由HTTP头部指示。
使用curl时,您需要添加:
-H "Accept: text/plain"
您还可以提供一个可接受的格式列表,服务器将以该列表中第一个支持的格式提供响应:
-H "Accept: text/plain, text/html, application/xml, application/json, */*"
发布于 2017-01-14 01:35:00
我从来没有尝试过compojure,所以这里什么都没有:
1.)您的本地val reps与别名命名空间同名--有点让人困惑
2.)要访问参数,您必须将ring.middleware.params/wrap-params应用于您的路由
3.)啊,是的,内容类型:因为你需要:form-params,由于缺少wrap-params而没有交付,所以你最终选择了某种默认路由-因此不是text/plain。至少我是这么想的。
使用
lein try compojure ring-server演示/粘贴到repl中:
(require '[compojure.core :refer :all])
(require '[ring.util.response :as resp])
(require '[ring.server.standalone :as server])
(require '[ring.middleware.params :refer [wrap-params]])
(def x
(POST "/echo" [message]
:summary "info log the input message and echo it back"
:description nil
:return String
:form-params [message :- String]
(let [resp (-> (resp/response (str "message: " message))
(resp/status 200)
(resp/header "Content-Type" "text/plain"))]
resp)))
(defroutes app (wrap-params x))
(server/serve app {:port 4042})测试:
curl -X POST -i --header 'Content-Type: application/x-www-form-urlencoded' -d 'message=frickin' 'http://localhost:4042/echo'
HTTP/1.1 200 OK
Date: Fri, 13 Jan 2017 17:32:03 GMT
Content-Type: text/plain;charset=ISO-8859-1
Content-Length: 14
Server: Jetty(7.6.13.v20130916)
message: frickinhttps://stackoverflow.com/questions/41626341
复制相似问题