2

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’
Alejandro Alcalde
  • 5,990
  • 6
  • 39
  • 79
  • 2
    I think that you'll get that error message if you somehow forgot to enable `OverloadedStrings`. The documentation you point to *does* include this, however, so it's not entirely clear why you'd get it. On the other hand, you don't write exactly how you try to run the program. – Mark Seemann Mar 24 '21 at 06:41
  • 3
    I was adding the LANGUAGE feature in the wrong place, a little bit more down and not at the top of the file. – Alejandro Alcalde Mar 24 '21 at 06:55

1 Answers1

3

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.

Willem Van Onsem
  • 443,496
  • 30
  • 428
  • 555
  • 2
    Arg, stupid me, I had the LANGUAGE feature a little bit more down on the file, not at the very top. Thanks!. And thanks also for the detailed explanation. – Alejandro Alcalde Mar 24 '21 at 06:55