haskellhttp-conduit

Couldn't match expected type ‘Request’ with actual type ‘[Char]’


I'm new to Haskel, I'm trying to run a simple example using http-conduit, the example is the one provided in their documentation.

However, when running the program I always get:

    • Couldn't match expected type ‘Request’ with actual type ‘[Char]’
    • In the first argument of ‘httpLBS’, namely
        ‘"http://httpbin.org/get"’
      In a stmt of a 'do' block:
        response <- httpLBS "http://httpbin.org/get"
      In the expression:
        do response <- httpLBS "http://httpbin.org/get"
           putStrLn
             $ "The status code was: " ++ show (getResponseStatusCode response)
           print $ getResponseHeader "Content-Type" response
           L8.putStrLn $ getResponseBody response
   |
12 |     response <- httpLBS "http://httpbin.org/get"
   |                         ^^^^^^^^^^^^^^^^^^^^^^^^

I've tried to create a project both with cabal and stack, add http-conduit and aeson as dependencies, but still getting the error.

Shouldn't the url be implicitly converted to a Request?

I've tried to import Request and try to create a Request from the url, but it complains:

import Network.HTTP.Client.Request

<no location info>: error:
    Could not load module ‘Network.HTTP.Client.Request’
    it is a hidden module in the package ‘http-client-0.6.4.1’

Solution

  • You need to enable the OverloadedStrings extension [schoolofhaskell]. You can add the LANGUAGE pragma to the top of the file, so:

    {-# LANGUAGE OverloadedStrings #-}
    
    -- …

    This extension will implicitly add fromString :: IsString a => String -> a to each string literal (not to be confused with an expression with type String). It makes it more convenient to work with string-like data such as for example Text.

    Beware however that the conversion is done at runtime, so if not all Strings map to a (valid) Request object, then you will only see this when the Request is evaluated.

    Shouldn't the url be implicitly converted to a Request?

    In Haskell, there are no implicit conversions, you always convert data through functions. The OverloadedStrings simply adds a implicit function call to the literals, and thus this means that a string literal now can take as type any type that is a member of the IsString type class.