Hi, I’m Erika Rowland (a.k.a. erikareads). Hi, I’m Erika. I’m an Ops-shaped Software Engineer, Toolmaker, and Resilience Engineering fan. I like Elixir and Gleam, Reading, and Design. She/Her. If you're looking for experienced talent, I would love to chat. Published on

Haskell Library - req

Notes on req

Snippets

  • Basic post requestUsing req ==3.13.0
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE OverloadedStrings #-}

module Main where

import Control.Monad.IO.Class (MonadIO (liftIO))
import Data.Aeson (FromJSON, ToJSON, Value)
import Data.Text (Text)
import GHC.Generics (Generic)
import Network.HTTP.Req
  ( IgnoreResponse,
    POST (POST),
    ReqBodyJson (ReqBodyJson),
    defaultHttpConfig,
    http,
    ignoreResponse,
    jsonResponse,
    port,
    req,
    responseBody,
    runReq,
    (/:),
  )
import Prelude

data MyData = MyData
  { message :: Text,
    datetime :: Text
  }
  deriving (Show, Generic)

instance ToJSON MyData

instance FromJSON MyData

main :: IO IgnoreResponse
main = runReq defaultHttpConfig $ do
  let myData =
        MyData
          { message = "haskell",
            datetime = "2023-10-14T14:20:00"
          }
  req
    POST
    (http "localhost" /: "api" /: "notification")
    (ReqBodyJson myData)
    ignoreResponse
    (port 4000)