我正在使用clj-http进行API调用,但是我得到了非常通用的错误消息。
ExceptionInfo clj-http: status 415 clj-http.client/wrap-exceptions/fn--1863 (client.clj:196) 如何查看API的完整响应?
发布于 2020-02-05 03:43:33
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,
; ...发布于 2020-02-05 08:37:00
HTTP客户端库似乎报告服务器拒绝了您的请求,HTTP状态代码为415。互联网工程任务组在RFC 7231中定义了超文本传输协议状态代码415。
https://stackoverflow.com/questions/60060742
复制相似问题