Skip to content

Instantly share code, notes, and snippets.

@Newlifer
Created November 6, 2015 10:15
Show Gist options
  • Save Newlifer/a7e08c249076ab8a3c65 to your computer and use it in GitHub Desktop.
Save Newlifer/a7e08c249076ab8a3c65 to your computer and use it in GitHub Desktop.
simpleHTTP usage example
import Network.HTTP
import Text.RawString.QQ
headers :: [Header]
headers = [
Header (HdrCustom "Connection") "keep-alive"
,Header (HdrCustom "Accept-Language") ";q=0.8,en-US;q=0.5,en;q=0.3"
,Header (HdrCustom "User-Agent") "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36"
,Header (HdrCustom "Accept") "application/json, text/javascript, */*; q=0.01"
,Header (HdrCustom "Accept-Encoding") "gzip, deflate"
]
get :: String -> IO String
get url = simpleHTTP (getRequest url) >>= getResponseBody
post :: String -> String -> String -> IO String
post url content_type body = simpleHTTP (postRequestWithBody url content_type body) { rqBody=body, rqHeaders=headers } >>= getResponseBody
postBody :: String
postBody = [r|{"khooy": "fornever"}|]
url :: String
url = "http://codingteam.org.ru"
contentType :: String
contentType = "application/json; charset=UTF-8"
main :: IO ()
main = do
response <- post url contentType postBody
putStr response
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment