我正在玩compojure-api,並在試圖管理Content-Type for我的簡單Web應用程序時被阻止。我想要的是發出一個純粹/文本的HTTP響應,但不知何故,Compojure-API一直將其設置爲「application/json」。如何在compojure響應中明確設置內容類型?
(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環形應用程序中獲得對Content-Type的控制權?
哪里哪里。沒有喜悅。我得到相同的結果。一些中間件正在破壞這一點。 Grrrrr。 –
嗯。它幾乎必須是中間件,但我不知道如何。我可以像https://github.com/metosin/compojure-api#api-with-schema-swagger-docs例子那樣引發這種行爲。這更像是一個堆棧中的錯誤;不尊重由處理程序設置的內容類型頭,而不是我的代碼邏輯中的錯誤。對於Content-Type這樣簡單的東西是一種挑戰,我感到非常驚訝。不過,感謝您的幫助。 –
看看:https://github.com/ngrunwald/ring-middleware-format/blob/master/src/ring/middleware/format_response.clj#L187。如果沒有匹配,它將選擇首選編碼器或第一個編碼器。由於compojure-api不支持純文本/文本,它將使用第一個是JSON:https:// github。com/metosin/compojure-api/blob/59b5d2271ac952ee6a7d3bad484f4e1510b18a59/src/compojure/api/api.clj#L26 –