The wai package

[Tags:library, mit, test]

API docs and the README are available at

[Skip to Readme]


Versions 0.0.0, 0.0.1, 0.1.0, 0.2.0, 0.2.1, 0.2.2,, 0.3.0, 0.3.1, 0.3.2, 0.4.0, 0.4.1, 0.4.2, 0.4.3, 1.0.0, 1.1.0,, 1.2.0,,,, 1.3.0,,,, 1.4.0,,, 1.4.1, 2.0.0, 2.1.0,,,, 3.0.0,,, 3.0.1,, 3.0.2,,,,,,, 3.2.0,, 3.2.1,
Change log
Dependencies base (==4.*), blaze-builder (>= && <0.5), bytestring (>=0.10), bytestring-builder (>= && <0.11), http-types (>=0.7), network (>=, text (>=0.7), transformers (>=0.0), vault (==0.3.*) [details]
License MIT
Author Michael Snoyman
Category Web
Home page
Source repository head: git clone git://
head: git clone git://
Uploaded Tue May 24 02:52:49 UTC 2016 by MichaelSnoyman
Distributions Arch:, Debian:, Fedora:, FreeBSD:, LTSHaskell:, NixOS:, Stackage:, Tumbleweed:
Downloads 98061 total (3265 in the last 30 days)
3 []
Status Docs uploaded by user
Build status unknown [no reports yet]
Hackage Matrix CI




Maintainer's Corner

For package maintainers and hackage trustees

Readme for wai

Readme for wai-

WAI: Web Application Interface

Getting started

You want a minimal example? Here it is!

{-# LANGUAGE OverloadedStrings #-}
import Network.Wai
import Network.HTTP.Types
import Network.Wai.Handler.Warp (run)

app :: Application
app _ respond = do
    putStrLn "I've done some IO here"
    respond $ responseLBS
        [("Content-Type", "text/plain")]
        "Hello, Web!"

main :: IO ()
main = do
    putStrLn $ "http://localhost:8080/"
    run 8080 app

Put that code into a file named hello.hs and install wai and warp from Hackage:

cabal install wai warp

Run it:

runhaskell hello.hs

Point your browser to:


Serving static content

We can modify our previous example to serve static content. For this create a file named index.html:

<p>Hello, Web!</p>

Now we redefine responseBody to refer to that file:

app2 :: Application
app2 _ respond = respond index

index :: Response
index = responseFile
    [("Content-Type", "text/html")]

Basic dispatching

An Application maps Requests to Responses:

ghci> :info  Application
type Application = Request -> IO Response

Depending on the path info provided with each Request we can serve different Responses:

app3 :: Application
app3 request respond = respond $ case rawPathInfo request of
    "/"     -> index
    "/raw/" -> plainIndex
    _       -> notFound

plainIndex :: Response
plainIndex = responseFile
    [("Content-Type", "text/plain")]

notFound :: Response
notFound = responseLBS
    [("Content-Type", "text/plain")]
    "404 - Not Found"

Doing without overloaded strings

For the sake of efficiency, WAI uses the bytestring package. We used GHCs overloaded strings to almost hide this fact. But we can easily do without. What follows is a more verbose definition of notFound, that works without GHC extensions:

import qualified Data.ByteString.Char8 as B8
import qualified Data.ByteString.Lazy.Char8 as LB8
import           Data.CaseInsensitive (mk)

notFound = responseLBS
    [(mk $ B8.pack "Content-Type", B8.pack "text/plain")]
    (LB8.pack "404 - Not Found")