Kostia R
Kostia R

Reputation: 2565

Return JSON from yesod handler

I'm trying to write a simplest JSON response from Yesod's handler, but have some really stupid error (apparently). My handler code is this:

-- HelloYesod/Handler/Echo.hs
module Handler.Echo where

import           Data.Aeson      (object, (.=))
import qualified Data.Aeson      as J
import           Data.Text       (pack)
import           Import
import           Yesod.Core.Json (returnJson)

getEchoR :: String -> Handler RepJson
getEchoR theText = do
  let json = object $ ["data" .= "val"]
  return json

Error is this:

Handler/Echo.hs:12:10:
    Couldn't match expected type `RepJson' with actual type `Value'
    In the first argument of `return', namely `json'
    In a stmt of a 'do' block: return json
    In the expression:
      do { let json = object $ ...;
           return json }
Build failure, pausing...

Upvotes: 7

Views: 1767

Answers (1)

Mark B.
Mark B.

Reputation: 131

I got caught by this one too: you just have to change your type signature and it will work:

getEchoR :: String -> Handler Value

My understanding is that the whole Rep system is deprecated in Yesod 1.2, so Handler's now return Html and Value rather than RepHtml and RepJson.

Hope this helps!

Upvotes: 6

Related Questions