Using halogen and payload together

I’m just starting out with purescript trying to write a simple purescript frontend to a Haskell backend, and am having issues getting them to communicate. I’m using purescript-payload, and I can communicate to the server just fine in the REPL, but as soon as I try to call it within my handleAction function I only get RequestError: { message: "There was a problem making the request: request failed" }. I’m not sure if I’m actually understanding where the error is coming from, but I was hoping someone who has some more experience might have some insight, or a minimal example of using a Halogen frontend with payload to see if I’m doing something obviously wrong.

I’m still struggling a little with why there’s a distinction between Aff and Effect, and thought maybe I need to do things differently when querying web APIs. My minimal example does things in a fairly synchronous manner at the moment.

In case someone might know I came up with a pretty minimal example that illustrates the differing behaviour.

module Example where

import Prelude
import Effect (Effect)
import Effect.Class (liftEffect)
import Effect.Class.Console (logShow)
import Effect.Aff (Aff, runAff)

import Halogen as H
import Halogen.Aff as HA
import Halogen.HTML as HH
import Halogen.VDom.Driver (runUI)

import Payload.Client (mkClient)
import Payload.Spec (Spec(Spec), GET)
import Payload.Client.Options (defaultOpts)

printToHtml :: Effect Unit
printToHtml = HA.runHalogenAff do
    body <- HA.awaitBody
    x <- callAPI
    runUI text (show x) body
  where
    text = H.mkComponent
        { initialState: identity
        , render: HH.text
        , eval: H.mkEval H.defaultEval
        }

callAPI :: Aff String
callAPI = do
    let client = mkClient opts spec
    map show $ client.google {}
  where
    spec :: Spec
        { google :: GET "/"
            { response :: String
            }
        }
    spec = Spec

    opts = defaultOpts { baseUrl = "http://www.google.ca" }

printToConsole :: Effect Unit
printToConsole = do
    _ <- runAff (\_ -> pure unit) do
       x <- callAPI
       liftEffect (logShow x)
    pure unit

Okay so I did figure out the issue. It was due to the fact that my server (and also Google in the minimal example) both didn’t have the Access-Control-Allow-Origin header set up to allow their content to be displayed on arbitrary web pages, which the console version didn’t care about, but the browser did. So not really a Halogen issue specifically so much as due to viewing the page in the browser.

1 Like

Yeah, I had the same issue today, I put the middleware in the Haskell backend to properly respond to all preflight OPTION requests properly and to set Access-Control-Allow-Origin: * in the actual GET/POST responses when necessary.

2 Likes