Search code examples
angularjsresthttpgrailsgroovy

Receiving Json data in groovy app


I´m working on this app which will be a frontend consuming data from other applications but in first stance, it will be posting credentials to another app already running in production, and after credentials are accepted it should redirect to that app with user logged in.

Here comes the problem. I´ve already tested sending data to the other application data is being received as

     params: [{"j_username":"username","j_password":"password","instance":"http:8080/TERA/authAuto"}:, action:authAuto, controller:login]
    username: null
    Prueba: null

I have tried to receive this as it follows all without success

request.JSON.j_username
params.j_username
params["j_username"]

The params: is actually params received by groovy being printed.

I´ll now add my angularJs code

vm.login = function(){
    $http({
        method: 'POST',
        url: "http://t0002161750:8080/TERA/authAuto",
        data: {j_username: vm.user.username, j_password: vm.user.password, instance: "http://t0002161750:8080/TERA/authAuto"},
        headers:{
        'Content-Type': 'application/x-www-form-urlencoded;charset=utf8'
        }
    }).success(function(response){
        $window.location.href = "http://t0002161750:8080/TERA/";
        });
    }
}

Im doing this tests with a companion having the other app running on his PC.

I may be doing something wrong conceptually speaking. I know that by sending the params in the $window.location.href = url+params will work but i dont want the credentials travelling in the url. I know i can encode them but lets try something else before giving up if it is possible.


Solution

  • The problem here is using the wrong Content-Type for the submission. The server will look for POST-vars in the body. The proper value to use is:

    Content-Type: application/json
    

    (instead of application/x-www-form-urlencoded;charset=utf8)