【问题标题】:How to perform an http request with clj-http including an api key?如何使用包含 api 密钥的 clj-http 执行 http 请求?
【发布时间】:2016-03-04 21:59:50
【问题描述】:

我正在尝试向 api 发出 http 请求,将给定的句子更改为 yoda 可能说的方式。这是我的代码,当前出现包含“缺少 Mashape 应用程序密钥”的错误。:

(ns clojure-noob.core
  (:gen-class)
  (:require [clj-http.client :as client]))

(defn api-request [method path body]
  (:body
    (client/request
      {:basic-auth "*MY-AUTH-KEY-HERE*"
       :method method
       :url (str "https://yoda.p.mashape.com" path)
       :content-type "text/plain"
       :body body})))

(api-request :get "/yoda?sentence=You+will+learn+how+to+speak+like+me+someday.++Oh+wait." "")

:basic-auth 部分是这段代码中最有问题的部分。 api描述位于:https://market.mashape.com/ismaelc/yoda-speak 这是这个 api 的工作 curl 请求的样子:

curl --get --include 'https://yoda.p.mashape.com/yoda?sentence=You+will+learn+how+to+speak+like+me+someday.++Oh+wait.' \
  -H 'X-Mashape-Key: *MY-AUTH-KEY-HERE*' \
  -H 'Accept: text/plain'

任何帮助都可能为我节省无数时间,让我在谷歌上寻找有关如何完成的线索。

【问题讨论】:

    标签: api clojure clj-http


    【解决方案1】:

    看起来密钥需要放在名为 X-Mashape-Key 的特定标头中,而不是使用 HTTP 基本身份验证。

    (client/request
      {:headers {"X-Mashape-Key" "*MY-AUTH-KEY-HERE*"}
       :method method
       :url (str "https://yoda.p.mashape.com" path)
       :content-type "text/plain"
       :body body})))
    

    【讨论】:

    • 供其他人参考,请关闭第二行打开的括号。
    • 我被抓住了!我发布了代码但没有运行它。我承认羞耻。 (现已修复)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-10-26
    • 2016-07-23
    • 2015-10-23
    • 1970-01-01
    • 2016-02-08
    • 1970-01-01
    • 2019-11-15
    相关资源
    最近更新 更多