2017-01-13 2 views
1

compojure-api로 놀고있어 간단한 웹 응용 프로그램의 Content-Type을 관리하려고 할 때 막혔습니다. 내가 원하는 것은 평범한/텍스트 인 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, */*"을 곱슬

당신은 추가해야

1) Y : 내가 지금 여기 compojure을 해본 적이

+0

아닌데 REPL에

lein try compojure ring-server 

데모/붙여 넣기와

. 기쁨이 없습니다. 나는 같은 결과를 얻는다. 어떤 미들웨어는 이것을 망쳐 놓고있다. 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/fire/fire. clj # L26 –

1

아무것도 간다

2. 혼란

의 종류)이 PARAMS에 대한 액세스를 얻을 - - 우리 지역의 발 reps는 앨리어스 네임 스페이스와 같은 이름을 가진 것 같다 - 당신은 당신의 경로

3.

) 아 ring.middleware.params/wrap-params를 적용해야 예 Content-Type : wrap-params이 누락되어 전달되지 않은 :form-params이 필요하므로 일종의 기본 경로로 끝 났으므로 text/plain이 아닙니다. 그게 내가 생각하는 것, 적어도.

(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 
관련 문제