0

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.

2
  • 1
    Looks like the errorPosition = 1:1 says that it received no input at all. Try Yesod's issue tracker. Dec 24, 2013 at 3:06
  • Okay. Do you know how to log/print the input Yesod recieves? Dec 24, 2013 at 3:32

1 Answer 1

3

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

2
  • Ahh, this is probably it. I'm a complete newbie when it comes to JS. Thanks! Dec 24, 2013 at 7:25
  • Figured it out. I had my Send button posting from Javascript, and as a form submit, so it was sending the data, and then re-sending a POST with an empty string... whoops. Dec 24, 2013 at 8:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.