• Stars
    star
    576
  • Rank 77,502 (Top 2 %)
  • Language
    Clojure
  • Created almost 13 years ago
  • Updated about 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A ClojureScript HTTP library.

cljs-http

https://img.shields.io/clojars/v/cljs-http.svg https://travis-ci.org/r0man/cljs-http.svg http://jarkeeper.com/r0man/cljs-http/status.svg https://jarkeeper.com/r0man/cljs-http/downloads.svg

A HTTP library for ClojureScript.

http://imgs.xkcd.com/comics/server_attention_span.png

Usage

Imports

cljs-http returns core.async channels, so you need to import the go macro and the <! function.

(ns example.core
  (:require-macros [cljs.core.async.macros :refer [go]])
  (:require [cljs-http.client :as http]
            [cljs.core.async :refer [<!]]))

Async response handling

All HTTP functions in cljs-http return a core.async channel. When a request has completed or failed it is put on that channel. You can take the response from that channel with the <! function within a go block.

(go (let [response (<! (http/get "https://api.github.com/users"
                                 {:with-credentials? false
                                  :query-params {"since" 135}}))]
      (prn (:status response))
      (prn (map :login (:body response)))))

You can pass your own response channel. This is useful when using a transducers.

(http/get "http://example.com" {:channel (chan 1 (map :body))})

Content type

cljs-http can serializes the body of a request and set the Content-Type header accordingly. You can send the body params in mutiple formats:

(http/post "http://example.com" {:edn-params {:foo :bar}})

(http/post "http://example.com" {:json-params {:foo :bar}})
;;(JSON is auto-converted via `cljs.core/clj->js`)

(http/post "http://example.com" {:transit-params {:key1 "value1" :key2 "value2"}})

(http/post "http://example.com" {:form-params {:key1 "value1" :key2 "value2"}})

To send form parameters (an array of values):

(http/post "http://example.com" {:form-params {:key1 [1 2 3] :key2 "value2"}})

To upload a file using Multipart parameters:

(http/post "http://example.com" {:multipart-params [["key1" "value1"] ["my-file" my-file]]})

Where my-file can be a JavaScript Blob instance

(def my-file (js/Blob. #js ["<h1>Hello</h1>"] #js {:type "text/html"}))

or a HTML file input field.

<input id="my-file" type="file">
(def my-file
  (-> (.getElementById js/document "my-file")
      .-files first))

If you want to set the name of the file use a vector as my-file.

(def my-file ["myfile" [value filename]])

See https://developer.mozilla.org/en-US/docs/Web/API/FormData/append#Syntax for more details.

HTTP Basic Authentication

(http/get "http://example.com" {:basic-auth {:username "hello" :password "world"}})

OAuth 2.0 Bearer Token

(http/get "http://example.com" {:with-credentials? false
                                :oauth-token "SecretBearerToken"})

Custom Headers (Authorization)

(http/post "https://example.com"
           {:with-credentials? false
            :headers {"Authorization" "SuperSecretToken"}})

Upload/Download progress monitoring

(let [progress-channel (async/chan)]
  (http/post "http://example.com" {:multipart-params [["key1" "value1"] ["my-file" my-file]] :progress progress-chan}))

The progress-channel will receive progress events: {:directon dir :loaded uploaded_or_downloaded :total size}

  • :direction is :upload or :download
  • in some cases :total can be missing

JSONP

(http/jsonp "http://example.com" {:callback-name "callback" :timeout 3000})

Where callback-name is used to specify JSONP callback param name. Defaults to β€œcallback”. timeout is the length of time, in milliseconds. This channel is prepared to wait for for a request to complete. If the call is not competed within the set time span, it is assumed to have failed.

License

Copyright (C) 2012-2016 r0man

Distributed under the Eclipse Public License, the same as Clojure.

More Repositories

1

sablono

Lisp/Hiccup style templating for Facebook's React in ClojureScript.
Clojure
696
star
2

sqlingvo

A Clojure & ClojureScript DSL for SQL
Clojure
210
star
3

inflections-clj

Rails-like inflection library for Clojure and ClojureScript
Clojure
207
star
4

ring-cors

Ring middleware for Cross-Origin Resource Sharing.
Clojure
165
star
5

oauth-clj

Clojure OAuth library
Clojure
93
star
6

geocoder-clj

A Clojure library for various geocoder services.
Clojure
43
star
7

soundklaus.el

Play music on SoundCloud with Emacs via EMMS
Emacs Lisp
42
star
8

svm-clj

A LibSVM wrapper for Clojure
Clojure
34
star
9

geo-clj

Experimental Clojure geo library.
Clojure
29
star
10

docopt.el

A Docopt implementation in Emacs Lisp.
Emacs Lisp
27
star
11

hdfs-clj

A Clojure library for the Hadoop Distributed File System
Clojure
27
star
12

.emacs.d

My Emacs configuration
Emacs Lisp
25
star
13

asahi-guix

Asahi Linux on GNU Guix
18
star
14

datumbazo

A JDBC driver for SQLingvo
Clojure
16
star
15

netcdf-clj

NetCDF Clojure Library
Clojure
14
star
16

closure-templates-clj

Clojure Library for Google's Closure Templates.
Clojure
11
star
17

grafeo

A GraphQL document and schema language based on S-expressions in Clojure & ClojureScript
Clojure
11
star
18

validation-clj

A validation library for Clojure.
Clojure
11
star
19

dotfiles

My Dot Files
Shell
10
star
20

commandline-clj

Clojure command line parsing library.
Clojure
9
star
21

noencore

Clojure/ClojureScript functions not in core.
Clojure
9
star
22

acts_as_rateable

Fork of Juixe Software's acts_as_rateable plugin.
Ruby
9
star
23

paimon.el

An Emacs mode for Splunk
Emacs Lisp
9
star
24

geonames-clj

Clojure API for GeoNames.
Clojure
8
star
25

sqlingvo.node

A ClojureScript driver for SQLingvo on Node.js.
Clojure
8
star
26

google-maps-clj

Google Maps Clojure Library
Clojure
8
star
27

postgis.spec

Clojure specs and generators for PostGIS types
Clojure
8
star
28

hive-el

Hive SQL mode extension for Emacs
Emacs Lisp
7
star
29

mahout-in-action

Clojure Examples from Mahout in Action
Clojure
7
star
30

hal-clj

A Clojure library to build HAL maps.
Clojure
6
star
31

acts_as_voteable

Fork of Juixe Software's acts_as_voteable plugin.
Ruby
5
star
32

migrate-clj

Rails-like Database Migration for Clojure
Clojure
5
star
33

rum-mdc

Material Design components for Rum
CSS
5
star
34

closure-lint-mode

An Emacs Mode for the Closure Linter
5
star
35

cascalog-nutch

Cascading Schemes and Cascalog helpers for Nutch sequence files.
Clojure
4
star
36

routes-clj

A Clojure & ClojureScript library to build url and path fns.
Clojure
4
star
37

lein-dpkg

Leiningen plugin for the Debian package management system
Clojure
4
star
38

flickrj

Java
4
star
39

freebase-clj

Clojure Library for Freebase.
Clojure
3
star
40

rest-clj

Clojure
2
star
41

request-clj

A HTTP library for Clojure & ClojureScript.
Clojure
2
star
42

diplomarbeit

Emacs Lisp
2
star
43

lein-env

Leiningen project environments.
Clojure
2
star
44

plz-media-type

A plz.el extension library to handle media types.
Emacs Lisp
2
star
45

conduit-rabbitmq

A library for building distributed applications in Clojure using RabbitMQ
Clojure
1
star
46

ovh-clj

A Clojure Library for the OVH Web Services.
Java
1
star
47

vertica-el

Vertica SQL mode extension for Emacs
Emacs Lisp
1
star
48

immutant-stackoverflow

Clojure
1
star
49

om-unmount-test

Clojure
1
star
50

relajso

Clojure
1
star
51

conduit

A library for stream processing in Clojure
Clojure
1
star
52

cascalog-printtap

A Cascalog tap that prints tuples via Clojure's pr-str function.
Clojure
1
star
53

arrows

A library for creating arrows in Clojure
Clojure
1
star
54

r0man.github.com

My GitHub pages.
1
star
55

guix-system

Scheme
1
star
56

sqlingvo.ksql

A Clojure DSL to build SQL statements for KSQL DB.
Clojure
1
star
57

ring-graphql-multipart

Ring middleware for GraphQL multipart form requests aka file uploads
Clojure
1
star