Jason Basanese
Jason Basanese

Reputation: 710

How to perform an http request with clj-http including an api key?

I am trying to make an http request to an api that changes a given sentence to the way yoda might say it. Here is my code that currently gets an error containing "Missing Mashape application key.":

(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." "")

The :basic-auth section is the most questionable part of this code. The api description is located here: https://market.mashape.com/ismaelc/yoda-speak This is what a working curl request for this api looks like:

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'

Any help is likely to save me countless more hours scraping google for clues on how this is done.

Upvotes: 1

Views: 1409

Answers (1)

Arthur Ulfeldt
Arthur Ulfeldt

Reputation: 91534

looks like the key needs to go in a specific header named X-Mashape-Key rather than use HTTP basic auth.

(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})))

Upvotes: 3

Related Questions