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
|
% wreq: a Haskell web client library
% HTTP made easy for Haskell.
<a href="tutorial.html" class="btn btn-primary btn-lg" role="button">Tutorial</a>
`wreq` is a library that makes HTTP client programming in Haskell
easy.
# Features
* Simple but powerful `lens`-based API
* Over 100 tests, and built on reliable libraries like [`http-client`](http://hackage.haskell.org/package/http-client/)
and [`lens`](https://lens.github.io/)
* Session handling includes connection keep-alive and pooling, and
cookie persistence
* Automatic decompression
* Powerful multipart form and file upload handling
* Support for JSON requests and responses, including navigation of
schema-less responses
* Basic and OAuth2 bearer authentication
* Amazon Web Services (AWS) request signing (Version 4)
* AWS signing supports sending requests through the
[Runscope Inc.](https://www.runscope.com) Traffic Inspector
# Whirlwind tour
All of the examples that follow assume that you are using the
[`OverloadedStrings`](https://downloads.haskell.org/~ghc/latest/docs/html/users_guide/type-class-extensions.html#overloaded-strings)
language extension, which you can enable in `ghci` as follows:
~~~~ {.haskell}
ghci> :set -XOverloadedStrings
~~~~
And now let's get started.
~~~~ {.haskell}
ghci> import Network.Wreq
ghci> r <- get "http://httpbin.org/get"
~~~~
The `wreq` library's `lens`-based API is easy to learn (the tutorial
walks you through the
[basics of lenses](tutorial.html#a-quick-lens-backgrounder)) and
powerful to work with.
~~~~ {.haskell}
ghci> import Control.Lens
ghci> r ^. responseHeader "Content-Type"
"application/json"
~~~~
Safely and sanely add query parameters to URLs. Let's find the most
popular implementations of Tetris in Haskell.
~~~~ {.haskell}
ghci> let opts = defaults & param "q" .~ ["tetris"]
& param "language" .~ ["haskell"]
ghci> r <- getWith opts "https://api.github.com/search/repositories"
~~~~
Haskell-to-JSON interoperation is seamless.
~~~~ {.haskell}
ghci> import GHC.Generics
ghci> import Data.Aeson
ghci> :set -XDeriveGeneric
ghci> data Addr = Addr Int String deriving (Generic)
ghci> instance ToJSON Addr
ghci> let addr = Addr 1600 "Pennsylvania"
ghci> post "http://httpbin.org/post" (toJSON addr)
~~~~
Work easily with schemaless JSON APIs. This traverses the complex
JSON search result we just received from GitHub above, and pulls out
the authors of our popular Tetris clones.
~~~~ {.haskell}
ghci> import Data.Aeson.Lens
ghci> r ^.. responseBody . key "items" . values .
key "owner" . key "login" . _String
["steffi2392","rmies","Spacejoker","walpen",{-...-}
~~~~
Easily write
[`attoparsec`](http://hackage.haskell.org/package/attoparsec) parsers
on the spot, to safely and reliably deal with complicated headers and
bodies.
~~~~ {.haskell}
ghci> import Data.Attoparsec.ByteString.Char8 as A
ghci> import Data.List (sort)
ghci> let comma = skipSpace >> "," >> skipSpace
ghci> let verbs = A.takeWhile isAlpha_ascii `sepBy` comma
ghci> r <- options "http://httpbin.org/get"
ghci> r ^. responseHeader "Allow" . atto verbs . to sort
ghci> ["GET","HEAD","OPTIONS"]
~~~~
There's a lot more, but why not jump in and start coding. In fact, if
you'd like to add new features, that would be great! We love pull
requests.
<div class="jumbotron" style="margin-top: 40px;">
<h2 style="margin-top: 20px;">Ready to jump in?</h2>
We've worked hard to make `wreq` quick to learn.
<a href="tutorial.html" class="btn btn-success btn-lg" role="button">Tutorial</a>
We're proud of the example-filled docs.
<a href="http://hackage.haskell.org/package/wreq" class="btn btn-info btn-lg" role="button">Documentation</a>
If you run into problems, let us know.
<a href="https://github.com/bos/wreq" class="btn btn-warning btn-lg" role="button">Issues</a>
</div>
# Acknowledgments
I'd like to thank Edward Kmett and Shachaf Ben-Kiki for tirelessly
answering my never-ending stream of
[lens](https://lens.github.io/)-related questions in `#haskell-lens`.
I also want to thank Michael Snoyman for being so quick with helpful
responses to bug reports and pull requests against his excellent
[http-client](http://hackage.haskell.org/package/http-client) package.
Finally, thanks to Kenneth Reitz for building the indispensable
[httpbin.org](http://httpbin.org/) HTTP testing service, and of course
for his [requests library](http://docs.python-requests.org/en/latest/).
|