2

I have a wai/warp application. How can I handle a post request? I have this:

app :: Application
app request respond = 
    respond $ case rawPathInfo request of
        "/"  -> responseFile status200 ............
        "/some_post_req/" -> .....

How can I specify that some_post_req must be POST?

Tsundoku
  • 2,455
  • 1
  • 19
  • 31
Otoma
  • 161
  • 2
  • 11
  • 1
    I see a `requestMethod` function in `Network.Wai` - would you be able to match on that result? – ryachza Apr 12 '17 at 13:22

2 Answers2

1

It should be as simple as comparing the result of Network.Wai.requestMethod against Network.Wai.methodPost:

app request respond
  | requestMethod request == methodPost
  = respond $ case rawPathInfo request of
    {- handle POST routes -}

  | otherwise
  = {- handle other request methods -}

Since there are constants for methodPost, methodGet, &c., you might as well use them, but Method is an alias for ByteString, so you could also use the OverloadedStrings extension:

{-# LANGUAGE OverloadedStrings #-}

And then either compare with a string literal:

requestMethod request == "POST"

Or pattern match:

case requestMethod request of
  "POST" -> {- … -}
  "GET" -> {- … -}
  …
Jon Purdy
  • 53,300
  • 8
  • 96
  • 166
0

parseRequestBody in the wai-extra package allows you to get the data you want out of the body of the request:

(params, files) <- parseRequestBody lbsBackEnd request

But this does nothing to specify that the request must be a POST request. Keep in mind that wai can be pretty low level, and there are higher level packages out there.

servant comes to mind as a package that allows you to define APIs at the type level. With servant, you definitely can specify the HTTP verb you expect. See here.

liminalisht
  • 1,243
  • 9
  • 11