Search code examples
haskellhaskell-stackio-monadscottywreq

How to display Response from an HTTP GET Request in front-end using Scotty?


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.


Solution

  • 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)