Last active
May 1, 2016 02:51
-
-
Save lotz84/25f1fde7c14b0cddcc2a58fb686a4611 to your computer and use it in GitHub Desktop.
SpockでPOSTリクエストを受け付けてみる
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ curl -XPOST -d'name=Alice' -d'age=15' http://localhost:8080/person | |
Person {name = "Alice", age = 15} | |
$ curl -XPOST -d'name=Bob' -d'age=16' http://localhost:8080/person | |
Person {name = "Bob", age = 16} | |
$ curl -XPOST -d'name=Carol' -d'age=abc' http://localhost:8080/person | |
Bad Request. | |
$ curl http://localhost:8080/person | |
Person {name = "Bob", age = 16} | |
Person {name = "Alice", age = 15} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# LANGUAGE OverloadedStrings #-} | |
module Main where | |
import Control.Monad.IO.Class (liftIO) | |
import Data.IORef | |
import Data.List (intercalate) | |
import qualified Data.Text as Text | |
import Network.HTTP.Types (status201, status400) | |
import Web.Spock.Safe | |
data Person = Person | |
{ name :: String | |
, age :: Int | |
} deriving Show | |
main :: IO () | |
main = do | |
personsRef <- newIORef [] | |
runSpock 8080 $ spockT id $ do | |
-- GET /person | |
get "person" $ do | |
persons <- liftIO $ readIORef personsRef | |
text . Text.pack . intercalate "\n" . map show $ persons | |
-- POST /person | |
post "person" $ do | |
maybeName <- param "name" | |
maybeAge <- param "age" | |
case Person <$> maybeName <*> maybeAge of | |
Nothing -> setStatus status400 >> text "Bad Request." | |
Just person -> do | |
liftIO $ modifyIORef personsRef (person:) | |
setStatus status201 | |
text . Text.pack $ show person |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: spock-post | |
version: 0.1.0.0 | |
build-type: Simple | |
cabal-version: >=1.10 | |
executable app | |
main-is: Main.hs | |
ghc-options: -threaded -rtsopts -with-rtsopts=-N | |
build-depends: base | |
, text | |
, transformers | |
, http-types | |
, Spock | |
default-language: Haskell2010 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
resolver: lts-5.12 | |
packages: | |
- '.' | |
extra-deps: [] | |
flags: {} | |
extra-package-dbs: [] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment