Joey Eremondi
Joey Eremondi

Reputation: 8423

Diagnosing "not enough bytes" when parsing JSON with Yesod

I'm trying to parse the response body of a Json POST request, using the following Yesod code.

import qualified Data.Aeson as J
postMypageR = do
  json <- parseJsonBody :: Handler (J.Result J.Value)
  case json of
       J.Error e -> error(show e) 
       J.Success code -> do
         liftIO $ putStrLn $ show code
         defaultLayout $ myWidget

In my Javascript, I send with xmlhttp.send(JSON.stringify({a:3, b:4}));

The Firefox web console confirms that the string sent to POST is {"a":3,"b":4}.

However, when I run the code, I get the following error: ParseError {errorContexts = [\"demandInput\"], errorMessage = \"not enough bytes\", errorPosition = 1:1}"

Does anybody know why this might be happening? My GETs and POSTs all work in other pages, but this is the only place in my app so far where I actually look at the POST body.

Upvotes: 0

Views: 176

Answers (1)

jamshidh
jamshidh

Reputation: 12070

My guess is that this is a javascript error.... Did you set the content length before the send?

  xmlhttp.setRequestHeader("Content-length", data.length);

where data=JSON.stringify({a:3, b:4})

Upvotes: 3

Related Questions