Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Interacting with a REST API from Clojure

What would be the suggested way to send and receive requests to an external REST API without having to run a web server? I can't seem to find anything about making requests and parsing the resulting JSON. The only thing I have found so far is just the json parsing stuff (using the Cheshire library).

Any help would be greatly appreciated!

like image 484
Tom Brunoli Avatar asked Oct 14 '12 22:10

Tom Brunoli


People also ask

How do I interact with restful API?

Step #1 – Enter the URL of the API in the textbox of the tool. Step #2 – Select the HTTP method used for this API (GET, POST, PATCH, etc). Step #3 – Enter any headers if they are required in the Headers textbox. Step #4 – Pass the request body of the API in a key-value pair.


1 Answers

A good library for interacting with an external REST API is clj-http, which uses Apache HTTPClient). For JSON, there are a few options: clojure.data.json (a core lib) and cheshire being some popular ones. The lib clj-http has cheshire as a dependency and has JSON support baked in. Cheshire makes use of Jackson.

For example, using clj-http:

(ns my.core   (:require [clj-http.client :as client]))  (client/put my-url   {:form-params body    :content-type :json    :oauth-token @token    :throw-exceptions false    :as :json}) 
like image 196
rplevy Avatar answered Sep 23 '22 21:09

rplevy