adeeshaek
adeeshaek

Reputation: 65

change (CurlCode, String) to String in Haskell

I am trying to create a simple haskell program which GETs a JSON string from a webserver and parses it.

I am using the curlGetString method of Network.Curl.

This method has the following type signature

curlGetString :: URLString -> [CurlOption] -> IO (CurlCode, String)

My question is: How do I convert the output from (CurlCode, String) to String?

Thanks!

Upvotes: 2

Views: 157

Answers (1)

Jon Purdy
Jon Purdy

Reputation: 54999

The response body is just the second element of the tuple. First bind the result of curlGetString in the IO monad, then use snd, or better, pattern matching:

main = do
  (code, body) <- curlGetString "http://foo.example.com/" [...]
  case code of
    CurlOK -> putStr body
    _ -> putStrLn $ "Error: " ++ show code

Don’t forget that you can find functions by type with a Hoogle search.

Upvotes: 3

Related Questions