|
1 | 1 | (ns clj-http.lite.client
|
2 |
| - "Batteries-included HTTP client." |
| 2 | + "Batteries-included HTTP client. |
| 3 | +
|
| 4 | + Among the many functions here you'll likely be most interested in |
| 5 | + [[get]] [[head]] [[put]] [[post]] [[delete]] or the slightly lower level [[request]]." |
3 | 6 | (:require [clj-http.lite.core :as core]
|
4 | 7 | [clj-http.lite.links :refer [wrap-links]]
|
5 | 8 | [clj-http.lite.util :as util]
|
|
233 | 236 | (client req))))
|
234 | 237 |
|
235 | 238 | (defn wrap-request
|
236 |
| - "Returns a battaries-included HTTP request function coresponding to the given |
237 |
| - core client. See client/client." |
| 239 | + "Returns a batteries-included HTTP request function." |
238 | 240 | [request]
|
239 | 241 | ;; note to the uninitiated: wrapper behaviour is applied to requests in order listed here but
|
240 | 242 | ;; from last to first
|
|
257 | 259 | wrap-links
|
258 | 260 | wrap-unknown-host))
|
259 | 261 |
|
260 |
| -(def #^{:doc |
261 |
| - "Executes the HTTP request corresponding to the given map and returns |
262 |
| - the response map for corresponding to the resulting HTTP response. |
263 |
| -
|
264 |
| - In addition to the standard Ring request keys, the following keys are also |
265 |
| - recognized: |
266 |
| - * :url |
267 |
| - * :method |
268 |
| - * :query-params |
269 |
| - * :basic-auth |
270 |
| - * :content-type |
271 |
| - * :accept |
272 |
| - * :accept-encoding |
273 |
| - * :as |
274 |
| -
|
275 |
| - The following additional behaviors over also automatically enabled: |
276 |
| - * Exceptions are thrown for status codes other than 200-207, 300-303, or 307 |
277 |
| - * Gzip and deflate responses are accepted and decompressed |
278 |
| - * Input and output bodies are coerced as required and indicated by the :as |
279 |
| - option."} |
| 262 | +(def ^{:arglists '([req])} |
280 | 263 | request
|
| 264 | + "Returns response map for executed HTTP `req` map. |
| 265 | +
|
| 266 | + Notice that some `req` key entries will be overwritten by automatic conversion to other key entries: |
| 267 | +
|
| 268 | + Request method |
| 269 | + * `:method` - ex. `:get`,`:head`,`:post`,`:put`,`:delete`, converts to `:request-method` with same value |
| 270 | +
|
| 271 | + Request URL |
| 272 | + * `:url` - ex. `\"https://joe:blow@example.com:443/some/path?q=clojure\"`, converts to: |
| 273 | + * `:scheme` - protocol `:https` |
| 274 | + * `:server-name` - host `\"example.com\"` |
| 275 | + * `:server-port` - `443` (if not specified, will be inferred from `:scheme`) |
| 276 | + * `:uri` - path `\"/some/path\"` |
| 277 | + * `:user-info` - `\"joe:blow\"`, converts to: |
| 278 | + * `:basic-auth` - which automatically converts to appropriate `:headers` |
| 279 | + * `:query-string` - `\"q=clojure\"` |
| 280 | + * `:query-params` - ex. `{\"q\" \"clojure\"}` or `{:q \"clojure\"}` converts to `:query-string` (see above) |
| 281 | +
|
| 282 | + Request body & headers |
| 283 | + * `:body` - can be a string, byte array, File or input stream |
| 284 | + * `:body-encoding` - charset ex. `\"UTF-16\"`, defaults to `\"UTF-8\"`, iff `:body` is string converts to: |
| 285 | + * `:body` encoded in charset |
| 286 | + * `:character-encoding` set to charset which converts to appropriate `:headers` iff `:content-type` also set |
| 287 | + * `:content-type` - media type of request body, converts to appropriate `:headers` entry, specify: |
| 288 | + * keyword as shorthand, ex. `:json` for `\"application/json\"` |
| 289 | + * string for verboten, ex. `\"text/html\"` |
| 290 | + * `:form-params` - ex. `{\"q\" \"clojure\"}` or `{:q \"clojure\"}`, iff `:method` is `:post`: converts to: |
| 291 | + * urlencoded `:body` |
| 292 | + * appropriate `:headers` entry |
| 293 | + * `:oauth-token` - bearer authorization token, ex. `\"my70k3nh3r3\"`, converts to appropriate `:headers` entry |
| 294 | + * `:basic-auth` - basic authentication, converts to appropriate `:headers` entry, (see also `:url` and `:user-info`), specify: |
| 295 | + * vector `[\"uname\" \"pass\"]` becomes `\"uname:pass\"` |
| 296 | + * use string for verboten |
| 297 | + * `:accept-encoding` - vector of accepted response encodings, ex. `[:gzip :deflate :identity]`, converts to appropriate `:headers` entry |
| 298 | + * `:accept` - accept response of media type, converts to appropriate `:headers` entry, specify |
| 299 | + * keyword as shorthand, ex. `:json` for `\"application/json\"` |
| 300 | + * string for verboten, ex. `\"text/html\"` |
| 301 | + * `:headers` - explicitly set request headers, ex. `{\"Cache-Control\" \"no-cache\"}` |
| 302 | +
|
| 303 | + Request behaviour |
| 304 | + * `:as` - specifies how response body should be coerced: |
| 305 | + * `:stream` |
| 306 | + * `:byte-array` |
| 307 | + * `:auto` - to string decoded with `charset` in response `:headers` `content-type` `charset` else UTF-8 |
| 308 | + * `\"charset\"` - to string decoded with `charset` ex. `\"utf-16\"` |
| 309 | + * else - to string decoded with UTF-8 |
| 310 | + * `:follow-redirects` - specify `false` to not follow response redirects |
| 311 | + * `:throw-exceptions` - specify `false` to not throw on https status error codes |
| 312 | + * `:ignore-unknown-host?` - specify `true` to not throw on unknown host |
| 313 | + * `:insecure?` - allow connection with an invalid SSL certificate |
| 314 | + * `:conn-timeout` - number of milliseconds to wait to establish a connection |
| 315 | + * `:socket-timeout` - number of milliseconds to wait for data to be available to read |
| 316 | + * `:save-request?` - specify `true` to include ultimate converted `:request` used in response map |
| 317 | + * `:chunk-size` - in bytes, enables streaming of HTTP request body with chunk-size bytes, |
| 318 | +see [JDK docs](https://docs.oracle.com/javase/8/docs/api/java/net/HttpURLConnection.html#setChunkedStreamingMode-int-) |
| 319 | +for details |
| 320 | +
|
| 321 | + Response map keys: |
| 322 | + * `:status` - http status code, see `:throw-exceptions` above |
| 323 | + * `:headers` - response headers |
| 324 | + * `:body` - response body, gzip and deflate responses are accepted and decompressed. See `:as` above. |
| 325 | + * `:request` - see `:save-request?` above |
| 326 | +
|
| 327 | + See [README](/README.md#usage) for example usages." |
281 | 328 | (wrap-request #'core/request))
|
282 | 329 |
|
283 | 330 | (defn get
|
284 |
| - "Like #'request, but sets the :method and :url as appropriate." |
| 331 | + "Executes HTTP GET request for `url` and, optionally, more `req` attributes. |
| 332 | + See [[request]]." |
285 | 333 | [url & [req]]
|
286 | 334 | (request (merge req {:method :get :url url})))
|
287 | 335 |
|
288 | 336 | (defn head
|
289 |
| - "Like #'request, but sets the :method and :url as appropriate." |
| 337 | + "Executes HTTP HEAD request for `url` and, optionally, more `req` attributes. |
| 338 | + See [[request]]." |
290 | 339 | [url & [req]]
|
291 | 340 | (request (merge req {:method :head :url url})))
|
292 | 341 |
|
293 | 342 | (defn post
|
294 |
| - "Like #'request, but sets the :method and :url as appropriate." |
| 343 | + "Executes HTTP POST request for `url` and, optionally, more `req` attributes. |
| 344 | + See [[request]]." |
295 | 345 | [url & [req]]
|
296 | 346 | (request (merge req {:method :post :url url})))
|
297 | 347 |
|
298 | 348 | (defn put
|
299 |
| - "Like #'request, but sets the :method and :url as appropriate." |
| 349 | + "Executes HTTP PUT request for `url` and, optionally, more `req` attributes. |
| 350 | + See [[request]]." |
300 | 351 | [url & [req]]
|
301 | 352 | (request (merge req {:method :put :url url})))
|
302 | 353 |
|
303 | 354 | (defn delete
|
304 |
| - "Like #'request, but sets the :method and :url as appropriate." |
| 355 | + "Executes HTTP DELETE request for `url` and, optionally, more `req` attributes. |
| 356 | + See [[request]]." |
305 | 357 | [url & [req]]
|
306 | 358 | (request (merge req {:method :delete :url url})))
|
307 | 359 |
|
|
0 commit comments