Restangular POST does not pass data parameters

I am new to using restangular. With that said, I have the following POST call that doesn't work as expected for some reason.

Restangular.all("user").all("login").post({username: 'test@user.com', password: 'xyz'}).then(function(account)
{ 
     console.log(account);
});

      

I see a POST request to the / api / v1 / user / login url, which is the correct url. However, the message parameters don't seem to get through. Looking at the same POST request, I can't see the username and password data. Also, my API is throwing an unauthorized error that tells me it cannot validate a user based on credentials.

I've also tried options like this:

Restangular.one("user").post("login", {username: 'test@user.com', password: 'xyz'}).then(function(account)
{ 
    console.log(account);

}) 

      

However, no postal data seems to be sent. I've also tried ...

Restangular.all("user").post("login", {username: 'test@user.com', password: 'xyz'}).then(function(account)
{ 
    console.log(account);

}) 

      

It seems that the wrong URL / api / v1 / user is being generated and no postal data is still visible.

I can make cURL POST requests to / api / v1 / user / login passing POST data with no problem.

cURL -X POST /api/v1/user/login -d "username=test@user.com&password=xyz"

      

I also updated my curl statement as @David suggested in his answer to ...

curl -X POST -d '{"username": "test@user.com", "password": "xyz"}' '/api/v1/user/login' -H Content-Type:application/json

      

What else worked. However, posting with a restate does not ...

Restangular.all("user").post("login", {username: 'test@user.com', password: 'xyz'}).then(function(account)
    { 
        console.log(account);

    }) 

      

Additional ideas?

+3


source to share


3 answers


I'm not an expert on Restangular, but it seems to .all()

create a complete set of REST APIs for an object. In this case, I think you only want to call .all()

on "user" and use the login as a subresource.

var users = Restangular.all("user");

users.post('login', {
  username: 'test@user.com',
  password: 'xyz'
}).then(...);

      



Also you need to make sure that your server has an endpoint POST /users/login

.

0


source


I see this annual question, but anyway. I faced the same problem and I figured I found a solution to this problem. It's all about the Restangular.setRequestInterceptor event. Therefore, if you have such an interceptor in your code, then you must be sure that it will return the received parameter of the element after the necessary actions have been taken. For example, what I had before this:

Restangular.setRequestInterceptor(function () {
            $rootScope.validationErrors = [];
            activeRequests++;
            $rootScope.loading = true;          
        })

      



And what you need to do with it:

Restangular.setRequestInterceptor(function (element, operation, what, url) {
        $rootScope.validationErrors = [];
        activeRequests++;
        $rootScope.loading = true;
        return element;
    })

      

0


source


You are sending the post parameters incorrectly, you have:

Restangular.all("user").all("login")
.post({username: 'test@user.com', password: 'xyz'})
.then(function(account) { 
    console.log(account); 
});

      

So, you need to change a little and it will work (send the body parameters correctly):

Restangular.all("user").all("login")
.post('', {username: 'test@user.com', password: 'xyz'})
.then(function(account) { 
    console.log(account); 
});

      

Note: I tried this on restangular v1.5.2

Good luck!

0


source







All Articles