无法使用 Clojure 发送内容类型为“x-www-form-urlencoded”的发布请求

问题描述 投票:0回答:1

我基本上尝试在 Clojure 中执行以下 cURL 命令:

curl --location '<auth-url>' \
--header 'Content-Type: application/x-www-form-urlencoded' \
--data-urlencode 'grant_type=client_credentials' \
--data-urlencode 'client_id=<client-ID>' \
--data-urlencode 'client_secret=-<client-secret>'
--data-urlencode 'audience=<audience>'

我已经在 Clojure 中尝试了以下操作,使用

[org.httpkit.client :as http]

(http/post auth-url {:form-params {:grant_type    "client_credentials"
                                   :client_id     client-id
                                   :client_secret client-secret
                                   :audience      audience}
                     :headers     {"Content-Type" "application/x-www-form-urlencoded"}})

我发现 cURL 命令在终端和 Postman 中都有效,但 Clojure 代码返回 403 Forbidden。我已经两次、三次检查了 url 和所有参数值是否正确。

我还尝试使用以下函数将表单参数作为查询参数编码并附加到 URL:

(defn make-query-string [m]
  (->> (for [[k v] m]
         (str (codec/url-encode k) "=" (codec/url-encode v)))
       (interpose "&")
       (apply str)))

(defn build-url [url-base query-map]
  (str url-base "?" (make-query-string query-map)))

但我得到了相同的 403 Forbidden 响应。

如果有人有任何指示,我无法弄清楚我在 Clojure 代码中做错了什么。谢谢!

http curl post clojure x-www-form-urlencoded
1个回答
0
投票

也许尝试将表单参数指定为字符串而不是键,并且不需要指定标题:

(let [opts {:form-params {"grant_type" "client_credentials"
                          "client_id" client-id
                          "client_secret" client-secret
                          "audience" audience}
      response @(http/post auth-url opts)]
  (-> response
      :body
      (json/decode true)))
© www.soinside.com 2019 - 2024. All rights reserved.