如何使用包含api密钥的clj-http执行http请求?

时间:2016-03-04 21:59:50

标签: api clojure clj-http

我正在尝试向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 --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'

任何帮助都可能为我节省数不清的时间来搜索谷歌如何完成这项工作的线索。

1 个答案:

答案 0 :(得分:3)

看起来需要进入名为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})))