[update methodM / methodOnly -> method Jeremy Shaw **20130625182248 Ignore-this: aa77db66449ac747865e0ef090c259eb ] hunk ./HelloWorld.lhs 132 -

ok is one of several combinators which can be used to set the HTTP response code. In this case, it will set the response code to 200 OK. Happstack.Server.SimpleHTTP contains similar functions for the common HTTP response codes including, notFound, seeOther, badRequest and more. These functions all act like the normal return function, except they also set the response code.

+

ok is one of several combinators which can be used to set the HTTP response code. In this case, it will set the response code to 200 OK. Happstack.Server.SimpleHTTP contains similar functions for the common HTTP response codes including, notFound, seeOther, badRequest and more. These functions all act like the normal return function, except they also set the response code.

hunk ./Makefile 7 -ROUTE_FILTER_DEMOS := MonadPlus.lhs Dir.lhs Dir2.lhs Dirs.lhs Path.lhs FromReqURI.lhs MethodM.lhs MatchMethod.lhs +ROUTE_FILTER_DEMOS := MonadPlus.lhs Dir.lhs Dir2.lhs Dirs.lhs Path.lhs FromReqURI.lhs Method.markdown.lhs Method.lhs MatchMethod.lhs hunk ./Makefile 26 -RouteFilters.html: $(ROUTE_FILTER_DEPS) +RouteFilters.html: $(ROUTE_FILTER_DEPS) $(ROUTE_FILTER_DEMOS) hunk ./MatchMethod.lhs 30 -> import Happstack.Server (Method(GET, HEAD), dir, methodM, nullConf, ok, simpleHTTP) +> import Happstack.Server (Method(GET, HEAD), dir, method, nullConf, ok, simpleHTTP) hunk ./MatchMethod.lhs 34 -> [ do methodM [GET, HEAD] +> [ do method [GET, HEAD] addfile ./Method.markdown.lhs hunk ./Method.markdown.lhs 1 + +Matching on request method `(GET, POST, etc)` +--------------------------------------------- + +We can specify that a route is only valid for specific HTTP request methods by using the `method` guard: + +
+#ifdef HsColour +method :: (ServerMonad m, MonadPlus m, MatchMethod method) => method -> m () +#endif +
+ +Here is a simple demo app: + +
+ + +> module Main where +> +> import Control.Monad (msum) +> import Happstack.Server (Method(GET, POST), dir, method, nullConf, ok, simpleHTTP) +> +> main :: IO () +> main = simpleHTTP nullConf $ msum +> [ do method GET +> ok $ "You did a GET request.\n" +> , do method POST +> ok $ "You did a POST request.\n" +> , dir "foo" $ do method GET +> ok $ "You did a GET request on /foo\n" +> ] + +
+ +Using `curl` we can see the expected results for normal `GET` and `POST` requests to `/`: + +
+
+     $ curl http://localhost:8000/
+    You did a GET request.
+     $ curl -d '' http://localhost:8000/
+    You did a POST request.
+
+
+ +Note that `method` does not require that all the segments of request path have been consumed. We can see in here that `/foo` is accepted, and so is `/foo/bar`. + +
+
+ $ curl http://localhost:8000/foo
+You did a GET request on /foo
+ $ curl http://localhost:8000/foo/bar
+You did a GET request on /foo
+
+
+ + +You can use `nullDir` to assert that all the path segments have been consumed: + +
+#ifdef HsColour +nullDir :: (ServerMonad m, MonadPlus m) => m () +#endif +
+ hunk ./MethodM.lhs 8 -

methodM and methodOnly

-

We can specify that a route is only valid for specific HTTP request methods by using the methodM guard:

+

method and methodOnly

+

We can specify that a route is only valid for specific HTTP request methods by using the method guard:

hunk ./MethodM.lhs 13 -> methodM :: (ServerMonad m, MonadPlus m, MatchMethod method) => method -> m () +> method :: (ServerMonad m, MonadPlus m, MatchMethod method) => method -> m () hunk ./MethodM.lhs 17 -

The methodM guard does two things:

+

The method guard does two things:

hunk ./MethodM.lhs 29 -> import Happstack.Server (Method(GET, POST), dir, methodM, nullConf, ok, simpleHTTP) +> import Happstack.Server (Method(GET, POST), dir, method, nullConf, ok, simpleHTTP) hunk ./MethodM.lhs 32 -> main = simpleHTTP nullConf $ msum -> [ do methodM GET +> main = simpleHTTP nullConf $ msum +> [ do method GET hunk ./MethodM.lhs 35 -> , do methodM POST +> , do method POST hunk ./MethodM.lhs 37 -> , dir "foo" $ do methodM GET +> , dir "foo" $ do method GET hunk ./MethodM.lhs 56 -

Note that methodM also requires that the all the segments of request path have been consumed. We can see in here that /foo is accepted, but not /foo/bar, since only foo is consumed by the, dir "foo" filter.

+

Note that method also requires that the all the segments of request path have been consumed. We can see in here that /foo is accepted, but not /foo/bar, since only foo is consumed by the, dir "foo" filter.

hunk ./MethodM.lhs 74 -

In fact the definition for methodM is simply:

+

In fact the definition for method is simply:

hunk ./MethodM.lhs 77 -> methodM :: (ServerMonad m, MonadPlus m, MatchMethod method) => method -> m () -> methodM meth = methodOnly meth >> nullDir +> method :: (ServerMonad m, MonadPlus m, MatchMethod method) => method -> m () +> method meth = methodOnly meth >> nullDir hunk ./RouteFilters.lhs 32 -#include "MethodM.lhs" +#include "Method.lhs" hunk ./RqDataPost.lhs 21 -> , toResponse, methodM +> , toResponse, method hunk ./RqDataPost.lhs 53 -> do methodM POST +> do method POST