[英]Clj-http exception: ExceptionInfo clj-http: status 415 clj-http.client/wrap-exceptions/fn--1863 (client.clj:196)
我正在使用 clj-http 進行 API 調用,但我收到了非常通用的錯誤消息。
ExceptionInfo clj-http: status 415 clj-http.client/wrap-exceptions/fn--1863 (client.clj:196)
如何查看 API 的完整響應?
clj-http的默認設置是在 >=400 狀態代碼上拋出異常。 因此,一旦您看到異常,請使用例如(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,
; ...
HTTP 客戶端庫似乎報告服務器以 HTTP 狀態代碼 415 拒絕了您的請求。 Internet 工程任務組在RFC 7231 中定義了 HTTP 狀態代碼 415。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.