Haskell Scotty and Angularjs: jsonData function stopped parsing json data sent with $http.post()

安稳与你 提交于 2019-12-24 11:10:47

问题


I have a Haskell/Scotty app with Angularjs as frontend. It worked flawlessly with regards to JSON parsing. Then it suddenly stopped for no reason. It happend after some Scotty and its dependencies version bump.

There is not much error feed back from the scotty jsonData function which parses JSON body. None of the POST requests from Angular work at the moment. I can't figure out what happened. I don't know which code example would be useful as any JSON POSTs result with jsonData - no parse error. That's all I get. Here are some code examples.

This is the controller that passes the simple user name and password

.controller('AuthCtrl', function($scope, authS, $rootScope) {
  $scope.auth = {local: false, uname: "", pass: ""};
  $scope.login = function() {
    authS.login($scope.auth);
  }
  $scope.logout = function() {
    $rootScope.user.uname = "";
    authS.logout();
  }

This is the service that sends the POST request JSON data to Scotty backend

.factory('authS', function($http, $location, flashS, $rootScope) {
  return {
    login: function (d) {
      $http.post("/api/login", d)
      .success(function() {
        $rootScope.user.uname = d.uname;
        flashS.set("Login Success!", 'green');
        $location.path('/home');
      })
      .error(function() {
        flashS.set("Login Failure!", 'red', true);
      });
    })

This is modified Scotty route function to try and catch the exception error

post "/api/login" $ loginUser `rescue` \e -> do
    b <- decodeUtf8 <$> body
    error $ unpack $ mconcat [ "Error was: ", e, "\n"
                             , "Body was:  ", b, "\n"
                             ]

This is the actual loginUser function

--------------------------------------------------------------------------------------
-- | Manage user login
loginUser :: ActionD ()
loginUser = do
  j <- jsonData
  error "passed the problem ..."
  let h = jdataDoc j
      u = at "uname" h
      p = at "pass" h
      l = at "local" h
  a <- if l then authDB u p else liftIO $ authAD u p
  if a then putInSession "uname" u else status unauthorized401

Nothing goes beyond j <- jsonData line.

jsonData function crashes and the exception gives me

Error was: jsonData - no parse: 
Body was:  

That's all I get. No useful error and not even the body to analyse.

If someone had a similar problems please help. Or maybe a suggestion on what could've gone wrong. As I said before it all worked perfectly before some stupid update.

Thank you.

UPDATE

Looks like I get an empty body. That's why json parse error is returned.

Changing the login function to this

loginUser :: ActionD ()
loginUser = do
  j <- body
  if not $ LBS.null j then error ("Body:" ++ (LBS.unpack j)) else error "No body"

Returns me an empty body.

At the same time looking at the payload from the Chrome console gives me normal body

Headers:

POST /api/login HTTP/1.1
Host: 0.0.0.0:3000
Connection: keep-alive
Content-Length: 42
Accept: application/json, text/plain, */*
Origin: http://0.0.0.0:3000
User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2062.120 Safari/537.36
Content-Type: application/json;charset=UTF-8
Referer: http://0.0.0.0:3000/login
Accept-Encoding: gzip,deflate
Accept-Language: en-US,en;q=0.8,ru;q=0.6,en-GB;q=0.4
Cookie: scottysid=1CVBcS09G4PKktG8XkyPHu8JjBj0FmhaltjmIprX

Body:

{"local":false,"uname":"Foo","pass":"Bar"}

Response headers:

Content-Type:text/plain; charset=utf-8
Date:Mon, 15 Sep 2014 07:40:45 GMT
Server:Warp/3.0.1.1

来源:https://stackoverflow.com/questions/25840699/haskell-scotty-and-angularjs-jsondata-function-stopped-parsing-json-data-sent-w

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!