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

问题描述:

我正在尝试向api发出一个http请求,该请求会将某个给定的句子更改为yoda可能会这么说的方式。这里是我的代码,目前获得包含一个错误“失踪Mashape应用的关键。”:如何使用包含api密钥的clj-http执行http请求?

(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' 

任何帮助可能我拯救无数多个小时刮谷歌对如何做到这一点的线索。

看起来像关键需要在名为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}))) 
+0

供其他人参考请关闭第二行打开的支架。 –

+0

我被抓住了!我发布的代码没有运行它。我承认*耻辱*。 (现在修复) –