2017-01-13 122 views
1

我正在玩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的控制權?

回答

1

compojure-api以HTTP客戶端請求的格式提供響應,該格式使用HTTP Accept標頭指示。

,捲曲你需要添加:

-H "Accept: text/plain"

您也可以提供可接受的格式列表,服務器將服務從列表中第一個支持的格式的響應:

-H "Accept: text/plain, text/html, application/xml, application/json, */*"

+0

哪里哪里。沒有喜悅。我得到相同的結果。一些中間件正在破壞這一點。 Grrrrr。 –

+0

嗯。它幾乎必須是中間件,但我不知道如何。我可以像https://github.com/metosin/compojure-api#api-with-schema-swagger-docs例子那樣引發這種行爲。這更像是一個堆棧中的錯誤;不尊重由處理程序設置的內容類型頭,而不是我的代碼邏輯中的錯誤。對於Content-Type這樣簡單的東西是一種挑戰,我感到非常驚訝。不過,感謝您的幫助。 –

+1

看看: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 –

1

我從來沒有試過compojure所以這裏什麼都不做:

1.)y我們當地的VAL reps具有相同的名稱作爲別名的命名空間 - 那種混亂

2),以獲得進入PARAMS - 它似乎 - 你必須申請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: frickin 
相關問題