0

I'm a bit of a noob with Angular and am having issues trying to post to a Drupal Services endpoint. I can post just fine with HttpRequester (FFox plugin), however all my attempts with Angular to post data to get a session result in 401 Unauthorized: missing required argument username or other errors.

Here is my testing factory resource with default input:

    userInfoApp.factory('LoginService', function($resource) {
      return $resource('/auth-service/user/login', {username: 'admin', password: 'admin'}, {
        update: {
          method: 'POST', // this method issues a POST request
          headers:{'Content-Type':'application/x-www-form-urlencoded; charset=UTF-8'}
        }
      });
    });

Here is the call I am making to it inside the controller (this):

    this.login = function() {
       var login = LoginService.update(function(data) {
         console.log(data);
       });
    };

So far this usually results in a query string generated like so:

http://project.loc/auth-service/user/login?password=admin&username=admin

and the response of:

401 Unauthorized : Missing required argument username

What might I be doing wrong here? I have gotten $resource to work just fine with other endpoints (like for a menu service to retrieve a menu) however posting seems to be much more finicky. Any suggestions would be appreciated.

Thanks

2
  • your $resource config seems to be using POST method.. and your URL have querystring? can you check if your Drupal Services endpoint can get those params? I believe your Drupal Services debug will not get the params! Commented May 25, 2015 at 3:48
  • 1
    No the service cannot get the params. And in fact, I wouldn't want it to. I want the POST to actually post. Login should not happen on the query string. Commented May 25, 2015 at 3:57

2 Answers 2

2

Your current configuration for $resource sends username & password as querystring. Hence they appear in your URL. I assume you need these values to be POST.

According to documentations all non GET methods have a payload parameter in the action method:

non-GET "class" actions: Resource.action([parameters], postData, [success], [error])

What you need to do is stop sending the parameters as default [parameters] and make use of the postData to POST data to your Drupal Services endpoint. You could do this when you call $update() as:

LoginService.update({}, {username: "admin",password: "admin"});
Sign up to request clarification or add additional context in comments.

1 Comment

Sorry, this didn't do it. Changed the service call to: this.login = function() { var login = LoginService.update({}, {username: "admin", password: "admin"}); }; And it had no impact.
2

Ok, I found a way that works; for the controller function:

    this.login = function() {
      var data = {
        username: this.username,
        password: this.password
      };
      var login = LoginService.save({}, data, 
        function(data) {
          // possibly do stuff with result
        },
        function(reply) {
          // Comments on best way to access $scope here are welcome.
          //  This feels a bit weird to me.
          $scope.info.errorMessage = reply.statusText;
        }  
      );
    };

And for the factory service:

      // Login factory service
      userInfoApp.factory('LoginService', function($resource) {
        return $resource('/auth-service/user/login');
      });

This actually retrieves the session id and other user data from the server.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.