使用clj-http.client调用ShipStation GET请求遇401错误求助
修正clj-http.client调用ShipStation的401错误问题
你的请求出现401认证错误,主要是三个核心问题导致,以下是具体修正方案:
错误点及对应修正
- 请求头配置键错误:clj-http中指定请求头的正确键是
:headers(复数),而非你使用的:header(单数),这会导致请求头未被正确加载。 - Authorization格式不符合标准:ShipStation采用HTTP Basic认证,需要将
username:password字符串做Base64编码后,添加Basic前缀,不能直接传递明文拼接的字符串。 - 异步调用参数结构错误:clj-http的异步回调需放在请求配置的
:success和:error字段中,同时要开启:async? true标识,不能作为独立参数传入。
修正后的完整代码
首先引入Java自带的Base64编码工具:
(import java.util.Base64)
构造认证头并发起请求(替换为你的ShipStation真实用户名和密码):
(def shipstation-auth (str "Basic " (.encodeToString (Base64/getEncoder) (.getBytes "your-username:your-password")))) (client/get "https://ssapi.shipstation.com/shipments?shipDateStart=2023-01-05&shipDateEnd=2023-01-05" {:async? true :headers {"Authorization" shipstation-auth ;; Host头可省略,clj-http会自动根据URL生成 "Host" "ssapi.shipstation.com"} :success (fn [response] (println "hello" (:body response))) :error (fn [exception] (println "exception message is: " (.getMessage exception)))})
额外说明
- Host头无需手动设置,clj-http会根据请求URL自动填充,若ShipStation有特殊要求可保留。
- 确保用户名和密码无多余空格,避免编码后认证失败。
内容的提问来源于stack exchange,提问作者SaharaStyle
相关产品推荐
相关产品推荐

