File: README.org

package info (click to toggle)
clj-http-clojure 2.3.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 408 kB
  • ctags: 2
  • sloc: xml: 263; makefile: 14
file content (1038 lines) | stat: -rw-r--r-- 35,432 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
#+TITLE:    clj-http documentation
#+AUTHOR:   Lee Hinman
#+STARTUP:  align fold nodlcheck lognotestate showall
#+OPTIONS:  H:4 num:nil toc:t \n:nil @:t ::t |:t ^:{} -:t f:t *:t
#+OPTIONS:  skip:nil d:(HIDE) tags:not-in-toc auto-id:t
#+PROPERTY: header-args :results code :exports both :noweb yes
#+HTML_HEAD: <style type="text/css"> body {margin-right:15%; margin-left:15%;} </style>
#+LANGUAGE: en

* Table of Contents                                                     :TOC:
:PROPERTIES:
:CUSTOM_ID: h:84c64317-1dfa-4955-bfa7-180745c31546
:END:
 - [[#introduction][Introduction]]
   - [[#overview][Overview]]
   - [[#philosophy-][Philosophy ]]
 - [[#installation][Installation]]
 - [[#quickstart][Quickstart]]
   - [[#head][HEAD]]
   - [[#get][GET]]
   - [[#put][PUT]]
   - [[#post][POST]]
   - [[#delete][DELETE]]
   - [[#coercions][Coercions]]
   - [[#headers][Headers]]
   - [[#meta-tag-headers][Meta Tag Headers]]
   - [[#link-headers][Link Headers]]
   - [[#redirects-][Redirects ]]
   - [[#cookies][Cookies]]
   - [[#exceptions][Exceptions]]
   - [[#decompression][Decompression]]
   - [[#debugging][Debugging]]
 - [[#authentication][Authentication]]
   - [[#basic-auth][Basic Auth]]
   - [[#digest-auth][Digest Auth]]
   - [[#oauth2][oAuth2]]
 - [[#advanced-usage][Advanced Usage]]
   - [[#raw-request][Raw Request]]
   - [[#persistent-connections][Persistent Connections]]
   - [[#proxies][Proxies]]
   - [[#custom-middleware][Custom Middleware]]
 - [[#development][Development]]
   - [[#faking-responses][Faking Responses]]
   - [[#optional-dependencies][Optional Dependencies]]
   - [[#clj-http-lite][clj-http-lite]]
   - [[#troubleshooting][Troubleshooting]]
 - [[#tests][Tests]]
 - [[#testimonials][Testimonials]]
 - [[#license][License]]

* Introduction
:PROPERTIES:
:CUSTOM_ID: h:5caf5111-96b3-401b-bba3-6b66cc625cbd
:END:

** Overview
:PROPERTIES:
:CUSTOM_ID: h:301e4e08-cd19-4066-888d-166f35d3f696
:END:

clj-http is an HTTP library wrapping the [[http://hc.apache.org/][Apache HttpComponents]] client. This
library has taken over from mmcgrana's clj-http.

[[https://secure.travis-ci.org/dakrone/clj-http.png]]

** Philosophy
:PROPERTIES:
:CUSTOM_ID: h:b8fdaffd-1f9f-4d08-93f7-ef50392e7af1
:END:

The design of =clj-http= is inspired by the [[http://github.com/mmcgrana/ring][Ring]] protocol for Clojure HTTP
 server applications.

The client in =clj-http.core= makes HTTP requests according to a given Ring
request map and returns [[https://github.com/ring-clojure/ring/blob/master/SPEC][Ring response maps]] corresponding to the resulting HTTP
response. The function =clj-http.client/request= uses Ring-style middleware to
layer functionality over the core HTTP request/response implementation. Methods
like =clj-http.client/get= are sugar over this =clj-http.client/request=
function.

* Installation
:PROPERTIES:
:CUSTOM_ID: h:280b3315-2b20-484d-962b-7f7132d20840
:END:

=clj-http= is available as a Maven artifact from [[http://clojars.org/clj-http][Clojars]].

With Leiningen/Boot:

#+BEGIN_SRC clojure
[clj-http "2.3.0"]
#+END_SRC

The previous major versions is available as:

#+BEGIN_SRC clojure
[clj-http "1.1.2"]
#+END_SRC

clj-http supports clojure 1.6.0 and higher.

* Quickstart
:PROPERTIES:
:CUSTOM_ID: h:67644772-7a82-451b-91b8-6cab871445b6
:END:

The main HTTP client functionality is provided by the =clj-http.client= namespace.

First, require it in the REPL:

#+BEGIN_SRC clojure
(require '[clj-http.client :as client])
#+END_SRC

Or in your application:

#+BEGIN_SRC clojure
(ns my-app.core
  (:require [clj-http.client :as client]))
#+END_SRC

The client supports simple =get=, =head=, =put=, =post=, =delete=, =copy=,
=move=, =patch=, and =options= requests. Response are returned as [[https://github.com/ring-clojure/ring/blob/master/SPEC][Ring-style
response maps]]:

** HEAD
:PROPERTIES:
:CUSTOM_ID: h:5db60716-9834-4658-9256-63732e69bed6
:END:

#+BEGIN_SRC clojure

(client/head "http://example.com/resource")

(client/head "http://site.com/resource" {:accept :json})

#+END_SRC

** GET
:PROPERTIES:
:CUSTOM_ID: h:b2c26b5c-a36a-4f65-9c70-5d9921a2390d
:END:

Example requests:

#+BEGIN_SRC clojure

(client/get "http://site.com/resources/id")

(client/get "http://site.com/resources/3" {:accept :json})

;; Specifying headers as either a string or collection:
(client/get "http://example.com"
  {:headers {"foo" ["bar" "baz"], "eggplant" "quux"}})

;; Using either string or keyword header names:
(client/get "http://example.com"
  {:headers {:foo ["bar" "baz"], :eggplant "quux"}})

;; Set any specific client parameters manually:
(client/post "http://example.com"
  {:client-params {"http.protocol.allow-circular-redirects" false
                   "http.protocol.version" HttpVersion/HTTP_1_0
                   "http.useragent" "clj-http"}})

;; Set your own cookie policy
(client/post "http://example.com"
  {:client-params {:cookie-policy (fn [cookie origin] (your-validation cookie origin))}})

;; Completely ignore cookies:
(client/post "http://example.com"
  {:client-params {:cookie-policy (constantly nil)}})

;; Need to contact a server with an untrusted SSL cert?
(client/get "https://alioth.debian.org" {:insecure? true})

;; If you don't want to follow-redirects automatically:
(client/get "http://site.come/redirects-somewhere" {:follow-redirects false})

;; Only follow a certain number of redirects:
(client/get "http://site.come/redirects-somewhere" {:max-redirects 5})

;; Throw an exception if redirected too many times:
(client/get "http://site.come/redirects-somewhere" {:max-redirects 5 :throw-exceptions true})

;; Throw an exception if the get takes too long. Timeouts in milliseconds.
(client/get "http://site.come/redirects-somewhere" {:socket-timeout 1000 :conn-timeout 1000})

;; Query parameters
(client/get "http://site.com/search" {:query-params {"q" "foo, bar"}})

;; "Nested" query parameters
;; (this yields a query string of `a[e][f]=6&a[b][c]=5`)
(client/get "http://site.com/search" {:query-params {:a {:b {:c 5} :e {:f 6})

;; Provide cookies — uses same schema as :cookies returned in responses
;; (see the cookie store option for easy cross-request maintenance of cookies)
(client/get "http://site.com"
  {:cookies {"ring-session" {:discard true, :path "/", :value "", :version 0}}})

;; Tell clj-http not to decode cookies from the response header
(client/get "http://example.com" {:decode-cookies false})

;; Support for IPv6!
(client/get "http://[2001:62f5:9006:e472:cabd:c8ff:fee3:8ddf]")

#+END_SRC

The client will also follow redirects on the appropriate =30*= status codes.

The client transparently accepts and decompresses the =gzip= and =deflate=
content encodings.

=:trace-redirects= will contain the chain of the redirections followed.

** PUT
:PROPERTIES:
:CUSTOM_ID: h:f28939e5-24af-4e0d-ac3d-81c52a271418
:END:

#+BEGIN_SRC clojure

(client/put "http://example.com/api" {:body "my PUT body"})

#+END_SRC

** POST
:PROPERTIES:
:CUSTOM_ID: h:f1454284-011f-4426-8cfc-c116da4301e0
:END:

#+BEGIN_SRC clojure

;; Various options:
(client/post "http://site.com/api"
  {:basic-auth ["user" "pass"]
   :body "{\"json\": \"input\"}"
   :headers {"X-Api-Version" "2"}
   :content-type :json
   :socket-timeout 1000  ;; in milliseconds
   :conn-timeout 1000    ;; in milliseconds
   :accept :json})

;; Send form params as a urlencoded body (POST or PUT)
(client/post "http://site.com" {:form-params {:foo "bar"}})

;; Send form params as a json encoded body (POST or PUT)
(client/post "http://site.com" {:form-params {:foo "bar"} :content-type :json})

;; Send form params as a json encoded body (POST or PUT) with options
(client/post "http://site.com" {:form-params {:foo "bar"}
                               :content-type :json
                               :json-opts {:date-format "yyyy-MM-dd"}})

;; You can also specify the encoding of form parameters
(client/post "http://site.com" {:form-params {:foo "bar"}
                                :form-param-encoding "ISO-8859-1"})

;; Send form params as a Transit encoded JSON body (POST or PUT) with options
(client/post "http://site.com" {:form-params {:foo "bar"}
                                :content-type :transit+json
                                :transit-opts
                                {:encode {:handlers {}}
                                 :decode {:handlers {}}}})

;; Send form params as a Transit encoded MessagePack body (POST or PUT) with options
(client/post "http://site.com" {:form-params {:foo "bar"}
                                :content-type :transit+msgpack
                                :transit-opts
                                {:encode {:handlers {}}
                                 :decode {:handlers {}}}})

;; Multipart form uploads/posts
;; takes a vector of maps, to preserve the order of entities, :name
;; will be used as the part name unless :part-name is specified
(client/post "http://example.org" {:multipart [{:name "title" :content "My Awesome Picture"}
                                              {:name "Content/type" :content "image/jpeg"}
                                              {:name "foo.txt" :part-name "eggplant" :content "Eggplants"}
                                              {:name "file" :content (clojure.java.io/file "pic.jpg")}]})

;; Multipart :content values can be one of the following:
;; String, InputStream, File, a byte-array, or an instance of org.apache.http.entity.mime.content.ContentBody
;; Some Multipart bodies can also support more keys (like :encoding
;; and :mime-type), check src/clj-http/multipart.clj to see all flags

;; Apache's http client automatically retries on IOExceptions, if you
;; would like to handle these retries yourself, you can specify a
;; :retry-handler. Return true to retry, false to stop trying:
(client/post "http://example.org" {:multipart [["title" "Foo"]
                                               ["Content/type" "text/plain"]
                                               ["file" (clojure.java.io/file "/tmp/missing-file")]]
                                   :retry-handler (fn [ex try-count http-context]
                                                    (println "Got:" ex)
                                                    (if (> try-count 4) false true))})

#+END_SRC

** DELETE
:PROPERTIES:
:CUSTOM_ID: h:23225b05-4fc1-48f1-995c-8daeaf4c7c90
:END:

#+BEGIN_SRC clojure

(client/delete "http://example.com/resource")

#+END_SRC

** Coercions
:PROPERTIES:
:CUSTOM_ID: h:44b36885-b8ea-4e78-9e04-5141995e6771
:END:
*** Input coercion
:PROPERTIES:
:CUSTOM_ID: h:b72eb0a0-5546-440b-95ea-ff10aa631fd8
:END:

#+BEGIN_SRC clojure
;; body as a byte-array
(client/post "http://site.com/resources" {:body my-byte-array})

;; body as a string
(client/post "http://site.com/resources" {:body "string"})

;; :body-encoding is optional and defaults to "UTF-8"
(client/post "http://site.com/resources"
             {:body "string" :body-encoding "UTF-8"})

;; body as a file
(client/post "http://site.com/resources"
             {:body (clojure.java.io/file "/tmp/foo") :body-encoding "UTF-8"})

;; :length is optional for passing in an InputStream; if not
;; supplied it will default to -1 to signal to HttpClient to use
;; chunked encoding
(client/post "http://site.com/resources"
             {:body (clojure.java.io/input-stream "/tmp/foo")})

(client/post "http://site.com/resources"
             {:body (clojure.java.io/input-stream "/tmp/foo") :length 1000})
#+END_SRC

*** Output coercion
:PROPERTIES:
:CUSTOM_ID: h:f617095a-dbda-40a8-8662-db62d0389fd5
:END:

#+BEGIN_SRC clojure
;; The default output is a string body
(client/get "http://site.com/foo.txt")

;; Coerce as a byte-array
(client/get "http://site.com/favicon.ico" {:as :byte-array})

;; Coerce as something other than UTF-8 string
(client/get "http://site.com/string.txt" {:as "UTF-16"})

;; Coerce as json
(client/get "http://site.com/foo.json" {:as :json})
(client/get "http://site.com/foo.json" {:as :json-strict})
(client/get "http://site.com/foo.json" {:as :json-string-keys})
(client/get "http://site.com/foo.json" {:as :json-strict-string-keys})

;; Coerce as Transit encoded JSON or MessagePack
(client/get "http://site.com/foo" {:as :transit+json})
(client/get "http://site.com/foo" {:as :transit+msgpack})

;; Coerce as a clojure datastructure
(client/get "http://site.com/foo.clj" {:as :clojure})

;; Coerce as x-www-form-urlencoded
(client/post "http://site.com/foo" {:as :x-www-form-urlencoded})

;; Try to automatically coerce the output based on the content-type
;; header (this is currently a BETA feature!). Currently supports
;; text, json and clojure (with automatic charset detection)
;; clojure coercion requires "application/clojure" or
;; "application/edn" in the content-type header
(client/get "http://site.com/foo.json" {:as :auto})

;; Return the body as a stream
(client/get "http://site.com/bigrequest.html" {:as :stream})
;; Note that the connection to the server will NOT be closed until the
;; stream has been read
#+END_SRC

JSON coercion defaults to only an "unexceptional" statuses, meaning status codes
in the #{200 201 202 203 204 205 206 207 300 301 302 303 307} range. If you
would like to change this, you can send the =:coerce= option, which can be set
to:

#+BEGIN_SRC clojure
:always        ;; always json decode the body
:unexceptional ;; only json decode when not an HTTP error response
:exceptional   ;; only json decode when it IS an HTTP error response
#+END_SRC

The =:coerce= setting defaults to =:unexceptional=.

** Headers
:PROPERTIES:
:CUSTOM_ID: h:5f5e2c8b-e9da-40ea-a5aa-2afc9fa4f2df
:END:

clj-http's treatment of headers is a little more permissive than the [[https://github.com/ring-clojure/ring/blob/master/SPEC][ring spec]]
specifies.

Rather than forcing all request headers to be lowercase strings,
clj-http allows strings or keywords of any case. Keywords will be
transformed into their canonical representation, so the :content-md5
header will be sent to the server as "Content-MD5", for instance.
String keys in request headers, however, will be sent to the server
with their casing unchanged.

Response headers can be read as keywords or strings of any case. If
the server responds with a "Date" header, you could access the value
of that header as :date, "date", "Date", etc.

If for some reason you require access to the original header name that
the server specified, it is available by invoking (keys ...) on the
header map.

This special treatment of headers is implemented in the
wrap-header-map middleware, which (like any middleware) can be
disabled by using with-middleware to specify different behavior.

** Query-string parameters
:PROPERTIES:
:CUSTOM_ID: h:dd49992c-a516-4af0-9735-4f4340773361
:END:

There are three different ways that query string parameters for array values can
be generated, depending on what the resulting query string should look like,
they are:

- A repeating parameter (default)
- Array style
- Indexed array style

Here is an example of the input and output for the ~:query_string~ parameter,
controlled by the ~:multi-param-style~ option:

#+BEGIN_SRC clojure
;; default style, with :multi-param-style unset
:a [1 2 3] => "a=1&a=2&a=3"
;; with :multi-param-style :array, a repeating param with array suffix
;; (PHP-style):
:a [1 2 3] => "a[]=1&a[]=2&a[]=3"
;; with :multi-param-style :indexed, a repeating param with array suffix and
;; index (Rails-style):
:a [1 2 3] => "a[0]=1&a[1]=2&a[2]=3"
#+END_SRC

** Meta Tag Headers
:PROPERTIES:
:CUSTOM_ID: h:1f1a4258-849f-4324-8687-d066c15de09b
:END:

HTML 4.01 allows using the tag ~<meta http-equiv="..." />~ and HTML 5 allows
using the tag ~<meta charset="..." />~ to specify a header that should be
treated as an HTTP response header. By default, clj-http will ignore the body of
the response (other than the regular output coercion), but if you need clj-http
to parse the headers out of the body, you can use the =:decode-body-headers=
option:

#+BEGIN_SRC clojure
;; without decoding body headers (defaults to off):
(:headers (client/get "http://www.yomiuri.co.jp/"))
=> {"server" "Apache",
    "content-encoding" "gzip",
    "content-type" "text/html",
    "date" "Tue, 09 Oct 2012 18:02:41 GMT",
    "cache-control" "max-age=0, no-cache",
    "expires" "Tue, 09 Oct 2012 18:02:41 GMT",
    "etag" "\"1dfb-2686-4cba2686fb8b1\"",
    "pragma" "no-cache",
    "connection" "close"}

;; with decoding body headers, notice the content-type,
;; content-style-type and content-script-type headers:
(:headers (client/get "http://www.yomiuri.co.jp/" {:decode-body-headers true}))
=> {"server" "Apache",
    "content-encoding" "gzip",
    "content-script-type" "text/javascript",
    "content-style-type" "text/css",
    "content-type" "text/html; charset=Shift_JIS",
    "date" "Tue, 09 Oct 2012 18:02:59 GMT",
    "cache-control" "max-age=0, no-cache",
    "expires" "Tue, 09 Oct 2012 18:02:59 GMT",
    "etag" "\"1dfb-2686-4cba2686fb8b1\"",
    "pragma" "no-cache",
    "connection" "close"}
#+END_SRC

This can be used to have clj-http correctly interpret the body's charset by
using:

#+BEGIN_SRC clojure
(client/get "http://www.yomiuri.co.jp/" {:decode-body-headers true :as :auto})
=> ;; correctly formatted :body (Shift_JIS charset instead of UTF-8)
#+END_SRC

Note that this feature is currently beta and uses [[https://github.com/weavejester/crouton][Crouton]] to parse the body of
the request. If you do not want to use this feature, you can include Crouton in
addition to clj-http as a dependency like so:

#+BEGIN_SRC clojure
(defproject foo "0.1.0-SNAPSHOT"
  :dependencies [[org.clojure/clojure "1.3.0"]
                 [clj-http "0.6.0"]
                 [crouton "1.0.0"]])
#+END_SRC

Note also that HEAD requests will not return a body, in which case this setting will have no effect.

clj-http will automatically disable the =:decode-body-headers= option.

** Link Headers
:PROPERTIES:
:CUSTOM_ID: h:338ed551-06d7-4889-91cd-b21aec21d15f
:END:

clj-http parses any [[http://tools.ietf.org/html/rfc5988][link headers]] returned in the response, and adds them to the
=:links= key on the response map. This is particularly useful for paging RESTful
APIs:

#+BEGIN_SRC clojure
(:links (client/get "https://api.github.com/gists"))
=> {:next {:href "https://api.github.com/gists?page=2"}
    :last {:href "https://api.github.com/gists?page=22884"}}
#+END_SRC

** Redirects
:PROPERTIES:
:CUSTOM_ID: h:0176f085-4ddb-4dfd-9007-d27a6e598ebd
:END:

clj-http conforms its behaviour regarding automatic redirects to the [[https://tools.ietf.org/html/rfc2616#section-10.3][RFC]].

It means that redirects on status =301=, =302= and =307= are not redirected on
methods other than =GET= and =HEAD=. If you want a behaviour closer to what most
browser have, you can set =:force-redirects= to =true= in your request to have
automatic redirection work on all methods by transforming the method of the
request to =GET=.

** Cookies
:PROPERTIES:
:CUSTOM_ID: h:82472506-4fbe-4c1d-9c09-b6f764647c24
:END:

*** Cookiestores
:PROPERTIES:
:CUSTOM_ID: h:d9887431-486f-456f-b698-3f708e46367f
:END:

clj-http can simplify the maintenance of cookies across requests if it is
provided with a _cookie store_.

#+BEGIN_SRC clojure
(binding [clj-http.core/*cookie-store* (clj-http.cookies/cookie-store)]
  (client/post "http://site.com/login" {:form-params {:username "..."
                                                      :password "..."}})
  (client/get "http://site.com/secured-page")
  ...)
#+END_SRC

(The =clj-http.cookies/cookie-store= function returns a new empty instance of a
default implementation of =org.apache.http.client.CookieStore=.)

This will allow cookies to only be _written_ to the cookie store. Cookies from
the cookie-store will not automatically be sent with future requests.

If you would like cookies from the cookie-store to automatically be sent with
each request, specify the cookie-store with the =:cookie-store= option:

#+BEGIN_SRC clojure
(let [my-cs (clj-http.cookies/cookie-store)]
  (client/post "http://site.com/login" {:form-params {:username "..."
                                                      :password "..."}
                                        :cookie-store my-cs})
  (client/post "http://site.com/update" {:body my-data
                                         :cookie-store my-cs}))
#+END_SRC

You can also us the =get-cookies= function to retrieve the cookies
from a cookie store:

#+BEGIN_SRC clojure
(def cs (clj-http.cookies/cookie-store))

(client/get "http://google.com" {:cookie-store cs})

(clojure.pprint/pprint (clj-http.cookies/get-cookies cs))
{"NID"
 {:domain ".google.com",
  :expires #<Date Tue Oct 02 10:12:06 MDT 2012>,
  :path "/",
  :value
  "58=c387....",
  :version 0},
 "PREF"
 {:domain ".google.com",
  :expires #<Date Wed Apr 02 10:12:06 MDT 2014>,
  :path "/",
  :value
  "ID=3ba...:FF=0:TM=133...:LM=133...:S=_iRM...",
  :version 0}}
#+END_SRC

*** Keystores, Trust-stores
:PROPERTIES:
:CUSTOM_ID: h:1546e3f1-3f9f-459a-9015-628afa22f59e
:END:

You can also specify your own keystore/trust-store to be used:

#+BEGIN_SRC clojure
(client/get "https://example.com" {:keystore "/path/to/keystore.ks"
                                   :keystore-type "jks" ; default: jks
                                   :keystore-pass "secretpass"
                                   :trust-store "/path/to/trust-store.ks"
                                   :trust-store-type "jks" ; default jks
                                   :trust-store-pass "trustpass"})
#+END_SRC

The =:keystore/:trust-store= values may be either paths to keystore
files or =KeyStore= instances.

** Exceptions
:PROPERTIES:
:CUSTOM_ID: h:dfb56fc9-a958-41ad-8de2-af15a4cd4902
:END:

The client will throw exceptions on, well, exceptional status codes, meaning all
HTTP responses other than =#{200 201 202 203 204 205 206 207 300 301 302 303
307}=. clj-http will throw a [[http://github.com/scgilardi/slingshot][Slingshot]] Stone that can be caught by a regular
=(catch Exception e ...)= or in Slingshot's =try+= block:

#+BEGIN_SRC clojure
(client/get "http://site.com/broken")
=> ExceptionInfo clj-http: status 404  clj-http.client/wrap-exceptions/fn--583 (client.clj:41)
;; Or, if you would like the Exception message to contain the entire response:
(client/get "http://site.com/broken" {:throw-entire-message? true})
=> ExceptionInfo clj-http: status 404 {:status 404,
                                       :headers {"server" "nginx/1.0.4",
                                                 "x-runtime" "12ms",
                                                 "content-encoding" "gzip",
                                                 "content-type" "text/html; charset=utf-8",
                                                 "date" "Mon, 17 Oct 2011 23:15 :36 GMT",
                                                 "cache-control" "no-cache",
                                                 "status" "404 Not Found",
                                                 "transfer-encoding" "chunked",
                                                 "connection" "close"},
                                       :body "...body here..."}
   clj-http.client/wrap-exceptions/fn--584 (client.clj:42

;; You can also ignore HTTP-status-code exceptions and handle them yourself:
(client/get "http://site.com/broken" {:throw-exceptions false})
;; Or ignore an unknown host (methods return 'nil' if this is set to
;; true and the host does not exist:
(client/get "http://aoeuntahuf89o.com" {:ignore-unknown-host? true})
#+END_SRC

(spacing added by me to be human readable)

How to use with Slingshot:

#+BEGIN_SRC
; Response map is thrown as exception obj.
; We filter out by status codes
(try+
  (client/get "http://some-site.com/broken")
  (catch [:status 403] {:keys [request-time headers body]}
    (log/warn "403" request-time headers))
  (catch [:status 404] {:keys [request-time headers body]}
    (log/warn "NOT Found 404" request-time headers body))
  (catch Object _
    (log/error (:throwable &throw-context) "unexpected error")
    (throw+)))
#+END_SRC

** Decompression
:PROPERTIES:
:CUSTOM_ID: h:1ff48808-dc42-46de-8cef-12983c446d80
:END:

By default, clj-http will add the ={"Accept-Encoding" "gzip, deflate"}= header
to requests, and automatically decompress the resulting gzip or deflate stream
if the =Content-Encoding= header is found on the response. If this is undesired,
the ={:decompress-body false}= option can be specified:

#+BEGIN_SRC clojure
;; Auto-decompression used: (google requires a user-agent to send gzip data)
(def h {"User-Agent" "Mozilla/5.0 (Windows NT 6.1;) Gecko/20100101 Firefox/13.0.1"})
(def resp (client/get "http://google.com" {:headers h}))
(:orig-content-encoding resp)
=> "gzip" ;; <= google sent response gzipped

;; and without decompression:
(def resp2 (client/get "http://google.com" {:headers h :decompress-body false})
(:orig-content-encoding resp2)
=> nil
#+END_SRC

If clj-http decompresses something, the "content-encoding" header is removed
from the headers (because the encoding is no longer true). This allows clj-http
to be used as a pass-through proxy with ring. The original content-encoding is
available as =:orig-content-encoding= in the response map if auto-decompression
is enabled.

** Debugging
:PROPERTIES:
:CUSTOM_ID: h:f86f4daa-356e-40ca-b87e-bf347ec1f38e
:END:

There are four debugging methods you can use:

#+BEGIN_SRC clojure
;; print request info to *out*:
(client/get "http://example.org" {:debug true})

;; print request info to *out*, including request body:
(client/post "http://example.org" {:debug true :debug-body true :body "..."})

;; save the request that was sent in a :request key in the response:
(client/get "http://example.org" {:save-request? true})

;; save the request that was sent in a :request key in the response,
;; including the body content:
(client/get "http://example.org" {:save-request? true :debug-body true})

;; add an HttpResponseInterceptor to the request. This callback
;; is called for each redirects with the following args:
;; ^HttpResponse resp, HttpContext^ ctx
;; this allows low level debugging + access to socket.
;; see http://hc.apache.org/httpcomponents-core-ga/httpcore/apidocs/org/apache/http/HttpResponseInterceptor.html
(client/get "http://example.org" {:response-interceptor (fn [resp ctx] (println ctx))})
#+END_SRC

* Authentication
:PROPERTIES:
:CUSTOM_ID: h:3c375d7a-7acc-45cb-a7a4-6f2bdf4cad95
:END:

** Basic Auth
:PROPERTIES:
:CUSTOM_ID: h:8ae77bcc-68c6-4560-affb-4bbe02c6b7a9
:END:

#+BEGIN_SRC

(client/get "http://site.com/protected" {:basic-auth ["user" "pass"]})
(client/get "http://site.com/protected" {:basic-auth "user:pass"})

#+END_SRC

** Digest Auth
:PROPERTIES:
:CUSTOM_ID: h:47c07a03-677f-4a4f-967f-242329a8ab07
:END:

#+BEGIN_SRC

(client/get "http://site.com/protected" {:digest-auth ["user" "pass"]})

#+END_SRC

** oAuth2
:PROPERTIES:
:CUSTOM_ID: h:e34482c0-15d2-483a-a183-d5e3f1f662a6
:END:

#+BEGIN_SRC

(client/get "http://site.com/protected" {:oauth-token "secret-token"})

#+END_SRC

* Advanced Usage
:PROPERTIES:
:CUSTOM_ID: h:e6aed224-7ed5-4340-bc4e-7874eefadd87
:END:

** Raw Request
:PROPERTIES:
:CUSTOM_ID: h:71bf84d3-2ff0-44a8-99aa-214d339cf7d2
:END:

A more general =request= function is also available, which is useful as a
primitive for building higher-level interfaces:

#+BEGIN_SRC clojure
(defn api-action [method path & [opts]]
  (client/request
    (merge {:method method :url (str "http://site.com/" path)} opts)))
#+END_SRC

*** Boolean options
:PROPERTIES:
:CUSTOM_ID: h:4a5870a5-693f-441d-a69c-da96eebbbb6e
:END:

Since 0.9.0, all boolean options can be expressed as either ={:debug true}= or
={:debug? true}=, with or without the question mark.

** Persistent Connections
:PROPERTIES:
:CUSTOM_ID: h:5f755de8-c106-4b89-aa0a-3074ef96efc4
:END:

clj-http can use persistent connections to speed up connections if multiple
connections are being used:

#+BEGIN_SRC clojure
(with-connection-pool {:timeout 5 :threads 4 :insecure? false :default-per-route 10}
  (get "http://aoeu.com/1")
  (post "http://aoeu.com/2")
  (get "http://aoeu.com/3")
  ...
  (get "http://aoeu.com/999"))
#+END_SRC

This is MUCH faster than sequentially performing all requests, because a
persistent connection can be used instead creating a new connection for each
request.

If you would prefer to handle managing the connection manager yourself, you can
create a connection manager yourself and specify it for each request:

#+BEGIN_SRC clojure
(def cm (clj-http.conn-mgr/make-reusable-conn-manager {:timeout 2 :threads 3}))
(def cm2 (clj-http.conn-mgr/make-reusable-conn-manager {:timeout 10 :threads 1}))

(get "http://aoeu.com/1" {:connection-manager cm2})
(post "http://aoeu.com/2" {:connection-manager cm})
(get "http://aoeu.com/3" {:connection-manager cm2})

;; Don't forget to shut it down when you're done!
(clj-http.conn-mgr/shutdown-manager cm)
(clj-http.conn-mgr/shutdown-manager cm2)
#+END_SRC

See the docstring on =make-reusable-conn-manager= for options and default
values.

** Proxies
:PROPERTIES:
:CUSTOM_ID: h:b5007a6f-f0bf-4d98-9ab9-b23fcebfa49a
:END:

A proxy can be specified by setting the Java properties: =<scheme>.proxyHost=
and =<scheme>.proxyPort= where =<scheme>= is the client scheme used (normally
'http' or 'https'). =http.nonProxyHosts= allows you to specify a pattern for
hostnames which do not require proxy routing - this is shared for all schemes.
Additionally, per-request proxies can be specified with the =proxy-host= and
=proxy-port= options (this overrides =http.nonProxyHosts= too):

#+BEGIN_SRC clojure
(client/get "http://foo.com" {:proxy-host "127.0.0.1" :proxy-port 8118})
#+END_SRC

You can also specify the =proxy-ignore-hosts= parameter with a list of
hosts where the proxy should be ignored. By default this list is
=#{"localhost" "127.0.0.1"}=.

A SOCKS proxy can be used by creating a proxied connection manager with
=clj-http.conn-mgr/make-socks-proxied-conn-manager=. Then using that connection
manager in the request.

For example if you wanted to connect to a local socks proxy on port =8081= you
would:

#+BEGIN_SRC clojure
(ns foo.bar
  (:require [clj-http.client :as client]
            [clj-http.conn-mgr :as conn-mgr]))

(client/get "https://google.com"
            {:connection-manager
             (conn-mgr/make-socks-proxied-conn-manager "localhost" 8081)})
#+END_SRC

You can also store the proxied connection manager and reuse it later.

** Custom Middleware
:PROPERTIES:
:CUSTOM_ID: h:afec8fd4-580a-4a82-9521-628f8fa4fbd8
:END:

Sometime it is desirable to run a request with some middleware enabled and some
left out, the =with-middleware= method provides this functionality:

#+BEGIN_SRC clojure
(with-middleware [#'clj-http.client/wrap-method
                  #'clj-http.client/wrap-url
                  #'clj-http.client/wrap-exceptions]
  (get "http://example.com")
  (post "http://example.com/foo" {:body (.getBytes "foo")}))
#+END_SRC

To see available middleware, check the =clj-http.client/default-middleware= var,
which is a vector of the default middleware that clj-http uses.
=clj-http.client/*current-middleware*= is bound to the current list of
middleware during request time.

* Development
:PROPERTIES:
:CUSTOM_ID: h:0fb11882-d060-4e2e-85f7-3bf18dc9051b
:END:

Please send a pull request or open an issue if you have any problems.

** Faking Responses
:PROPERTIES:
:CUSTOM_ID: h:8bacf773-9af1-4098-907e-f96a780d3fca
:END:

If you need to fake clj-http responses (for things like testing and such), check
out the [[https://github.com/myfreeweb/clj-http-fake][clj-http-fake]] library.

** Optional Dependencies
:PROPERTIES:
:CUSTOM_ID: h:a847429d-741f-4a5a-8d27-916ea1017461
:END:

In 2.0.0+ clj-http's optional dependencies at excluded by default, in order to
use the features you will need to add them to your =project.clj= file.

clj-http currently has four optional dependencies, =cheshire=, =crouton=,
=tools.reader= and =ring/ring-codec=. Any number of them may be included by
adding them with the clj-http dependency in your project.clj:

#+BEGIN_SRC clojure
;; optional dependencies
[cheshire] ;; for :as :json
[crouton] ;; for :decode-body-headers
[org.clojure/tools.reader] ;; for :as :clojure
[ring/ring-codec] ;; for :as :x-www-form-urlencoded
#+END_SRC

Prior to 2.0.0, you can /exclude/ the dependencies and clj-http will work
without them.

** clj-http-lite
:PROPERTIES:
:CUSTOM_ID: h:472e4fba-3c50-4eb6-95fb-95d0d9afdbad
:END:

Like clj-http but need something more lightweight without as many external
dependencies? Check out [[https://github.com/hiredman/clj-http-lite][clj-http-lite]] for a project that can be used as a
drop-in replacement for clj-http.

** Troubleshooting
:PROPERTIES:
:CUSTOM_ID: h:e97ba275-3324-4102-9beb-9bcbc483ad15
:END:
*** VerifyError class org.codehaus.jackson.smile.SmileParser overrides final method getBinaryValue...
:PROPERTIES:
:CUSTOM_ID: h:048d8994-647a-4325-ab5d-f96fa12d5798
:END:

This is actually caused by your project attempting to use [[https://github.com/mmcgrana/clj-json/][clj-json]] and [[https://github.com/dakrone/cheshire][cheshire]]
in the same classloader. You can fix the issue by either not using clj-json (and
thus choosing cheshire), or specifying an exclusion for clj-http in your project
like this:

#+BEGIN_SRC clojure
(defproject foo "0.1.0-SNAPSHOT"
  :dependencies [[org.clojure/clojure "1.3.0"]
                 [clj-http "0.3.4" :exclusions [cheshire]]])
#+END_SRC

Note that if you exclude cheshire, json decoding of response bodies
and json encoding of form-params cannot happen, you are responsible
for your own encoding/decoding.

As of clj-http 0.3.5, you should no longer see this, as Cheshire 3.1.0
and clj-json can now live together without causing problems.

*** NoHttpResponseException ... due to stale connections**
:PROPERTIES:
:CUSTOM_ID: h:c2a2ea17-d402-43ba-b57b-2a5bc75a6750
:END:

Persistent connections kept alive by the connection manager become stale: the
target server shuts down the connection on its end without HttpClient being able
to react to that event, while the connection is being idle, thus rendering the
connection half-closed or 'stale'.

This can be solved by using (with-connection-pool) as described in the
'Using Persistent Connection' section above.

* Tests
:PROPERTIES:
:CUSTOM_ID: h:34bd658a-26a2-4731-9b7d-dd93bce8c35a
:END:

To run the tests:

#+BEGIN_SRC
$ lein deps
$ lein test

Run all tests (including integration):
$ lein test :all

Run tests against 1.2.1, 1.3 and 1.4
$ lein all test
$ lein all test :all
#+END_SRC

* Testimonials
:PROPERTIES:
:CUSTOM_ID: h:3e19427d-7d2b-465e-8fa4-1d59f9555924
:END:

With close to a [million](https://clojars.org/clj-http) downloads, clj-http is a
widely used, battle-tested clojure library. It is also included in other
libraries (like database clients) as a low-level http wrapper.

Libraries using clj-http:

- [[https://github.com/mattrepl/clj-oauth] [clj-oauth]]
- [[[[https://github.com/clojurewerkz/elastisch]]] [elasticsearch]]
- [[https://github.com/olauzon/capacitor] [influxdb]]

Libraries inspired by clj-http:

- [[https://github.com/mpenet/jet] [jet]]
- [[https://github.com/hiredman/clj-http-lite] [clj-http-lite]]

* License
:PROPERTIES:
:CUSTOM_ID: h:9968d81c-ff40-40f5-be27-60bab27c64c9
:END:

Released under the MIT License:
<http://www.opensource.org/licenses/mit-license.php>