The Author Online Book Forums are Moving

The Author Online Book Forums will soon redirect to Manning's liveBook and liveVideo. All book forum content will migrate to liveBook's discussion forum and all video forum content will migrate to liveVideo. Log in to liveBook or liveVideo with your Manning credentials to join the discussion!

Thank you for your engagement in the AoF over the years! We look forward to offering you a more enhanced forum experience.

376034 (11) [Avatar] Offline
#1
From the code samples it seems that the author is importing and using ByteString.Char8 when its suggested that we avoid using that to preserver unicode.

I have commented out ByteString.Char8 and it seems to be working fine with ByteString. What am i missing here?

module Main where

import Lib
   
import qualified Data.ByteString as B
--import qualified Data.ByteString.Char8 as BC
import qualified Data.ByteString.Lazy as L
--import qualified Data.ByteString.Lazy.Char8 as LC
import Network.HTTP.Simple

myToken :: B.ByteString
myToken = "xxx"
 
noaaHost :: B.ByteString
noaaHost = "www.ncdc.noaa.gov"
 
apiPath :: B.ByteString
apiPath = "/cdo-web/api/v2/datasets"

buildRequest :: B.ByteString -> B.ByteString -> B.ByteString -> B.ByteString -> Request
buildRequest token host method path  = setRequestMethod method
                                     $ setRequestHost host
                                     $ setRequestHeader "token" [token]
                                     $ setRequestPath path
                                     $ setRequestSecure True
                                     $ setRequestPort 443
                                     $ defaultRequest

request :: Request
request = buildRequest myToken noaaHost "GET" apiPath

main :: IO ()
main = do
  response <- httpLBS request
  let status = getResponseStatusCode response
  if status == 200
    then do
      print "saving request to file"
      let jsonBody = getResponseBody response
      L.writeFile "data.json" jsonBody
  else print "request failed with error"