0

I'm trying out Scotty for the first time and I can't seem to get past making my GET request. The Response is returned as type

IO (Response bytestring-0.10.8.1:Data.ByteString.Lazy.Internal.ByteString)

I know I need to convert it to a type that can be output by Scotty but I can't figure out how to do that.

My full code is :

{-# LANGUAGE DeriveGeneric     #-}
{-# LANGUAGE OverloadedStrings #-}
module Main where

import           Control.Lens
import           Control.Monad.IO.Class
import           Data.Aeson             (FromJSON, ToJSON, Value, decode,
                                         encode)
import           Data.Map               as Map
import           GHC.Generics
import           Lib
import           Network.Wreq           as Wreq
import           Web.Scotty             as Scotty

main :: IO ()
main =
  scotty 3000 $
   Scotty.get "/" $ do
    -- html "Hello World!"
     Wreq.get"https://www.metaweather.com/api/location/search/?query=New%20York"

I tried using LiftIO but that is still giving me a Type Error. I wanted to know how exactly I should convert my Response so that I can display it in the front-end just like I displayed my initial "Hello World" with html.

Chad Gilbert
  • 36,115
  • 4
  • 89
  • 97

1 Answers1

1

If you are just looking for a quick proof of concept and aren't worried about erroneous responses, you could use the responseBody lens and send the lazy byte string to raw instead of html:

main :: IO ()
main =
  scotty 3000 $
   Scotty.get "/" $ do
     r <- liftIO $ Wreq.get "https://www.metaweather.com/api/location/search/?query=New%20York"
     raw (r ^. responseBody)
Chad Gilbert
  • 36,115
  • 4
  • 89
  • 97
  • Thanks a lot for your reply. However I was still getting an error with this code. I edited the second last line to include LiftIO to make it run correctly. r <- liftIO $ Wreq.get "https://www.metaweather.com/api/location/search/?query=New%20York" – Kahlil Abreu Aug 09 '17 at 14:39
  • Is there any quick and easy way to convert a ByteString to text? (which is what i would need to use html instead of raw) – Kahlil Abreu Aug 09 '17 at 14:42
  • 1
    Woops! I forgot the `liftIO`. Thanks, I've updated the answer – Chad Gilbert Aug 09 '17 at 14:45
  • The [`string-conversions`](https://hackage.haskell.org/package/string-conversions-0.4.0.1/docs/Data-String-Conversions.html) package's [`cs`](https://hackage.haskell.org/package/string-conversions-0.4.0.1/docs/Data-String-Conversions.html#v:cs) function is a pretty easy way to convert. e.g `cs (r ^. responseBody) :: Text` – Chad Gilbert Aug 09 '17 at 14:51