Created
October 17, 2013 07:38
-
-
Save askucher/7020681 to your computer and use it in GitHub Desktop.
JSON RESTful Http (GET,POST) client on haskell
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
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