Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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...
like image 502
Konstantine Rybnikov Avatar asked Jun 24 '13 18:06

Konstantine Rybnikov


1 Answers

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!

like image 67
Mark B. Avatar answered Nov 03 '22 02:11

Mark B.