Skip to content

Instantly share code, notes, and snippets.

@askucher
Created October 17, 2013 07:38
Show Gist options
  • Save askucher/7020681 to your computer and use it in GitHub Desktop.
Save askucher/7020681 to your computer and use it in GitHub Desktop.
JSON RESTful Http (GET,POST) client on haskell
module Http(get,post) where
import Network.Socket
--import Network.HTTP
import Network.HTTP.Conduit
import Network.HTTP.Types.Method
import Network.HTTP.Types.Header
--import Control.Monad
--import Control.Applicative
import qualified Data.ByteString.Lazy.Char8 as C
import qualified Data.ByteString.Char8 as B
getResponse req = withSocketsDo $ do
res <- withManager $ httpLbs req
let body = C.unpack . responseBody $ res
return body
get url = withSocketsDo $ do
initReq <- parseUrl url
let req = initReq { method = methodGet }
body <- getResponse req
return body
post (url ,json) = withSocketsDo $ do
initReq <- parseUrl url
let req = initReq { method = methodPost
, requestHeaders = [(hContentType, B.pack "application/json")]
, requestBody = RequestBodyLBS . C.pack $ json
}
body <- getResponse req
return body
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment