Note that there are some explanatory texts on larger screens.

plurals
  1. POHaskell Aeson with HTTP Request
    text
    copied!<p>I'm a complete newbie to Haskell and having some trouble parsing JSON from a url. I have managed to code the parsing sides of things and have tested with Strings and they work fine. The trouble I'm getting is when I start working with IO. </p> <p>I have the following code :</p> <pre><code>data Movie = Movie { adult :: Bool, backdrop_path :: Maybe String, id :: Int, original_title :: String, release_date :: String, poster_path :: Maybe String, popularity :: Int, title :: String, vote_average :: Double, vote_count :: Int } deriving (Show) data MovieList = MovieList {getMovies ::[Movie]} deriving (Show) instance FromJSON Movie where parseJSON (Object v) = Movie &lt;$&gt; (v .: "adult") &lt;*&gt; (v .:? "backdrop_path") &lt;*&gt; -- Optional (v .: "id") &lt;*&gt; (v .: "original_title") &lt;*&gt; (v .: "release_date") &lt;*&gt; (v .:? "poster_path") &lt;*&gt; -- Optional (v .: "popularity") &lt;*&gt; (v .: "title") &lt;*&gt; (v .: "vote_average") &lt;*&gt; (v .: "vote_count") instance FromJSON MovieList where parseJSON (Object o) = MovieList &lt;$&gt; o .: "results" parseJSON _ = mzero movieAPIRequest :: String -&gt; IO String movieAPIRequest movieURI = do resp &lt;- simpleHTTP request case resp of Left x -&gt; return $ "Error connecting: " ++ show x Right r -&gt; case rspCode r of (2,_,_) -&gt; return $ rspBody r -- Request Fulfilled _ -&gt; return $ show r -- Request Failed where request = Request {rqURI = uri, rqMethod = GET, rqHeaders = [], rqBody = ""} uri = fromJust $ parseURI movieURI convertToByteString s = BS.pack s main = do response &lt;- movieAPIRequest url decoded &lt;- decode (convertToByteString response):: Maybe MovieList return $ decoded </code></pre> <p>I can't get the main to work. I want to automatically retrieve JSON from a url. The <code>movieAPIRequest</code> gives me the body of the request (JSON) as <code>IO String</code>. <code>convertToByteString</code> takes a String and converts to Data.ByteString.Lazy.Char8.ByteString as the <code>decode</code> function in Aeson takes a bytestring as an argument. With the above code I'm getting the following error:</p> <p>[1 of 1] Compiling MovieDataType ( MovieDataType.hs, interpreted )</p> <pre><code>MovieDataType.hs:62:20: Couldn't match type `Maybe' with `IO' Expected type: IO MovieList Actual type: Maybe MovieList In a stmt of a 'do' block: decoded &lt;- decode (convertToByteString response) :: Maybe MovieList In the expression: do { response &lt;- movieAPIRequest url; decoded &lt;- decode (convertToByteString response) :: Maybe MovieList; return $ decoded } In an equation for `main': main = do { response &lt;- movieAPIRequest url; decoded &lt;- decode (convertToByteString response) :: Maybe MovieList; return $ decoded } Failed, modules loaded: none. </code></pre> <p>I've tried fixing it but keep getting different that I can't get my head around. For example this one is telling its expecting IO MovieList but decode should return a Maybe MovieList.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload