Skip to content

Instantly share code, notes, and snippets.

@dariooddenino
Created July 29, 2016 16:17
Show Gist options
  • Save dariooddenino/3c1aa70888040c4b86ba84094db8ce80 to your computer and use it in GitHub Desktop.
Save dariooddenino/3c1aa70888040c4b86ba84094db8ce80 to your computer and use it in GitHub Desktop.
post request
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