Created
July 29, 2016 16:17
-
-
Save dariooddenino/3c1aa70888040c4b86ba84094db8ce80 to your computer and use it in GitHub Desktop.
post request
This file contains 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
data Post = Post { id :: Maybe Int | |
, title :: String | |
, body :: String | |
, userId :: Int | |
} | |
instance decodeJsonPost :: DecodeJson Post where | |
decodeJson json = do | |
obj <- decodeJson json | |
id <- obj .? "id" | |
title <- obj .? "title" | |
body <- obj .? "body" | |
userId <- obj .? "userId" | |
pure $ Post { id, title, body, userId } | |
instance encodeJsonPost :: EncodeJson Post where | |
encodeJson (Post post) | |
= "id" := post.id | |
~> "title" := post.title | |
~> "body" := post.body | |
~> "userId" := post.userId | |
~> jsonEmptyObject | |
post1 = Post { id: Nothing, title: "title", body: "body", userId: 20 } | |
main :: forall e. Eff (err :: EXCEPTION, ajax :: AJAX, console :: CONSOLE | e) Unit | |
main = void $ launchAff $ do | |
res <- post "http://jsonplaceholder.typicode.com/posts" (encodeJson post1) | |
liftEff $ log $ "response: " <> res.response |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment