1

我正在使用 clj-http 进行 API 调用,但我收到了非常通用的错误消息。

ExceptionInfo clj-http: status 415 clj-http.client/wrap-exceptions/fn--1863 (client.clj:196) 

如何查看 API 的完整响应?

4

2 回答 2

2

clj-http的默认设置是在 >=400 状态码上抛出异常。因此,一旦您看到异常,请深入挖掘 eg(pst)或您通常如何处理异常。例如

(c/get "https://httpbin.org/status/400")
; Execution error (ExceptionInfo) at slingshot.support/stack-trace (support.clj:201).
; clj-http: status 400
(pst)
; ...
; clojure.lang.ExceptionInfo: clj-http: status 400
;                      body: ""
;                    cached: nil
;                  chunked?: false
;                   headers: {"Date" "Tue, 04 Feb 2020 19:37:14 GMT",
;                             "Content-Type" "text/html; charset=utf-8",
;                             "Content-Length" "0",
;                             "Connection" "close",
;                             "Server" "gunicorn/19.9.0",
;                             "Access-Control-Allow-Origin" "*",
;                             "Access-Control-Allow-Credentials" "true"}
;               http-client: #object[org.apache.http.impl.client.InternalHttpClient 0x3cc148d "org.apache.http.impl.client.InternalHttpClient@3cc148d"]
;                    length: 0
;     orig-content-encoding: nil
;          protocol-version: {:name "HTTP", :major 1, :minor 1}
;             reason-phrase: "BAD REQUEST"
;               repeatable?: false
;              request-time: 429
;                    status: 400
;                streaming?: false
;           trace-redirects: []
;                      type: :clj-http.client/unexceptional-status
; ...

如果您不想要此功能,可以使用以下选项禁用它:throw-exceptions false

(c/get "https://httpbin.org/status/400" {:throw-exceptions false})
; {:body "",
;  :cached nil,
;  :chunked? false,
;  :headers {"Access-Control-Allow-Credentials" "true",
;            "Access-Control-Allow-Origin" "*",
;            "Connection" "close",
;            "Content-Length" "0",
;            "Content-Type" "text/html; charset=utf-8",
;            "Date" "Tue, 04 Feb 2020 19:40:20 GMT",
;            "Server" "gunicorn/19.9.0"},
;  :http-client #<org.apache.http.impl.client.InternalHttpClient@27bde886>,
;  :length 0,
;  :orig-content-encoding nil,
;  :protocol-version {:major 1, :minor 1, :name "HTTP"},
;  :reason-phrase "BAD REQUEST",
;  :repeatable? false,
;  :request-time 432,
;  :status 400,
;  :streaming? false,
;  :trace-redirects []}

或者如果你真的想看看发生了什么,你也可以使用:debug true让底层的 http 库记录“一切”:

(c/get "https://httpbin.org/status/400" {:debug true :throw-exceptions false})
; Request: nil
; {:user-info nil,
;  :use-header-maps-in-response? true,
;  :body-type nil,
;  :debug true,
;  :headers {"accept-encoding" "gzip, deflate"},
;  :server-port nil,
;  :url "https://httpbin.org/status/400",
;  :flatten-nested-keys (:query-params),
;  :throw-exceptions false,
;  :uri "/status/400",
;  :server-name "httpbin.org",
;  :query-string nil,
;  :body nil,
;  :scheme :https,
;  :request-method :get}
; HttpRequest:
; {:config nil,
;  :method "GET",
;  :requestLine
;  #object[org.apache.http.message.BasicRequestLine 0x648100de "GET https://httpbin.org/status/400 HTTP/1.1"],
;  :aborted false,
;  :params
;  #object[org.apache.http.params.BasicHttpParams 0x3d1319b "org.apache.http.params.BasicHttpParams@3d1319b"],
;  :protocolVersion
;  #object[org.apache.http.HttpVersion 0x34b63def "HTTP/1.1"],
;  :URI
;  #object[java.net.URI 0x51ba8929 "https://httpbin.org/status/400"],
;  :class org.apache.http.client.methods.HttpGet,
;  :allHeaders
;  [#object[org.apache.http.message.BasicHeader 0x39a8905b "Connection: close"],
;   #object[org.apache.http.message.BasicHeader 0x6d6c4775 "accept-encoding: gzip, deflate"]]}
; {:body "",
;  :cached nil,
;  :chunked? false,
; ...
于 2020-02-04T19:43:33.063 回答
0

HTTP 客户端库似乎报告服务器拒绝了您的 HTTP 状态代码 415 的请求。互联网工程任务组在RFC 7231中定义了 HTTP 状态代码 415 。

于 2020-02-05T00:36:59.930 回答