Skip to content

Instantly share code, notes, and snippets.

@jasonjohnson
Last active December 14, 2015 14:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jasonjohnson/5101078 to your computer and use it in GitHub Desktop.
Save jasonjohnson/5101078 to your computer and use it in GitHub Desktop.
(ns httpjson.core
(:require [clj-http.client :as client]))
(defn create-db
[db]
(client/request {:as :json
:accept :json
:content-type :json
:method :put
:url (str "http://127.0.0.1:5984/" db)
:throw-exceptions false}))
(defn -main
[& args]
(prn (create-db "mydb"))
(prn (create-db "mydb")))
# First attempt, decodes body to a map
{:trace-redirects ["http://127.0.0.1:5984/mydb"], :request-time 175, :status 201, :headers {"server" "CouchDB/1.2.0 (Erlang OTP/R15B01)", "location" "http://127.0.0.1:5984/mydb", "date" "Wed, 06 Mar 2013 17:13:12 GMT", "content-type" "application/json", "content-length" "12", "cache-control" "must-revalidate"}, :body {:ok true}}
# Second attempt, normally throws an exception. With :throw-exceptions false, body is a string.
{:trace-redirects ["http://127.0.0.1:5984/mydb"], :request-time 5, :status 412, :headers {"server" "CouchDB/1.2.0 (Erlang OTP/R15B01)", "date" "Wed, 06 Mar 2013 17:13:12 GMT", "content-type" "application/json", "content-length" "95", "cache-control" "must-revalidate"}, :body "{\"error\":\"file_exists\",\"reason\":\"The database could not be created, the file already exists.\"}\n"}
(defproject httpjson "0.0.0"
:main httpjson.core
:dependencies [[org.clojure/clojure "1.4.0"]
[clj-http "0.6.4"]])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment