1

I (New to Haskell) am trying to perform unpack operation on ByteString that I receive from webpage. Basically I want to search few words from webpage so I am trying to tokenize stream, then search word from words.

Prelude Network.HTTP.Conduit LB> LB.unpack (simpleHttp WebLink)

But I am getting below error

<interactive>:75:12: error:
• Couldn't match expected type ‘LB.ByteString’
              with actual type ‘m0 LB.ByteString’
• In the first argument of ‘LB.unpack’, namely...

From hackage I can see that its signature is

unpack :: ByteString -> [Word8] Source
O(n) Converts a ByteString to a '[Word8]'.
Manvi
  • 1,136
  • 2
  • 18
  • 41

2 Answers2

3

simpleHttp "http://example.com" is of type m ByteString, for some monad m, so for example of type IO ByteString. Using do notation you can get at the result.

import Network.HTTP.Conduit
import qualified Data.ByteString.Lazy.Char8 as LB

main :: IO ()
main = do
  res <- simpleHttp "http://example.com"
  let string = LB.unpack res
  putStr string

Or in ghci,

ghci> res <- simpleHttp "http://example.com"
ghci> LB.unpack res
luqui
  • 59,485
  • 12
  • 145
  • 204
mb21
  • 34,845
  • 8
  • 116
  • 142
2

simpleHttp WebLink appears to be a monadic action that returns a value, it's not a ByteString itself. You must run the procedure, obtaining the value, then (assuming it is a bytestring) you can unpack it.

Note that the simpleHttp procedure I know of does not return a bytestring. You'll want to pattern match on the return value to inspect the Either type, if it is a response message instead of a failure then you can further pattern match on the response.

Thomas M. DuBuisson
  • 64,245
  • 7
  • 109
  • 166
  • Thanks Thomas for your reply and explanation of problem. Can you please suggest some link or some example to do what you suggested? – Manvi Nov 07 '16 at 17:56