public
Last active

  • Download Gist
core.clj
Clojure
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
(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")))
out.txt
1 2 3 4 5 6 7 8
# 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"}
project.clj
Clojure
1 2 3 4
(defproject httpjson "0.0.0"
:main httpjson.core
:dependencies [[org.clojure/clojure "1.4.0"]
[clj-http "0.6.4"]])

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.