Search code examples
javascripthttpdvue.jsvibed

Logout from site cause error: 400 - Bad Request


I am using vibed.org framework. When I am processing logout function, I am getting strange error.

Here is my App.d code:

void main()
{
    auto router = new URLRouter;
    router.any("/checkAuthorization", &checkAuthorization);
    router.any("/login", &login);
    router.any("/logout", &logout);
    // ..
}

...

    void logout(HTTPServerRequest req, HTTPServerResponse res)
    {
        logInfo("Logout section");
        Json request = req.json;
        Json answerJSON = Json.emptyObject;         
        if (req.session) // if user have active session
        {
            res.terminateSession();
            answerJSON["status"] = "success";
            answerJSON["isAuthorized"] = false;
            res.writeJsonBody(answerJSON);

            logInfo(answerJSON.toString);

            logInfo("User %s logout", request["username"]); //
        }
    else
    {
        answerJSON["status"] = "fail"; // user do not have active session?
        logInfo("User do not have active session"); 
    }
}

And Vue.JS code:

  function logout()
  {
      var loginData = new Object();
      //data that we take from user input
      loginData["username"] = this.username; // username more then enough
      console.log("Logout username -> " + loginData["username"]);

    this.$http.post('http://127.0.0.1:8080/logout', loginData["username"]).then(function (response) {
        console.log("server response: ", response.data)
        if(response.data["isAuthorized"] == false)
        {
          console.log("Logout from site success");
          App.topMenuView = 'guestmenu' //Change current view!
          userLoginNotification("Goodbye, " + loginData["username"], "User Logout"); // notificate user
        }
    });

  }

But I am getting in Chrome console error:

Uncaught (in promise) Object {request: Object, data: "400 - Bad Request Bad Request Internal error information: std.json.JSONException@C:\vibe-d-0.7.27-alpha.1\source\vibe\data\json.d(1100): (0): Error: Expected 'true', got 'test'. ---------------- 0x0044ABF0 in pure @safe bool std.exception.enforceEx!(std.json.JSONException).enforceEx!(bool).enforceEx(bool, lazy immutable(char)[], immutable(char)[], uint) core.thread.Fiber.run()", status: 400, statusText: "Bad Request", ok: false}

And I can't understand what's wrong. It's look like it's issue on server side, because logInfo("Logout section"); is unreachable.


Solution

  • Your're sending string as loginData["username"] instead of {username:loginData["username"]} василий