Search code examples
angularjscurl-commandline

how can i convert my curl command to angularjs http request


Need to convert below curl command into angular http request. command looks similar to below command.

" curl -X POST --data 'username=myusername&password=mypassword' -i https://myurl.com/j_spring_security_check "

In https what I suppose to use, GET or POST. Can any one help in this.


Solution

  • It could be a service that has a postMethod method that takes two arguments username and password just like this :

    angular.factory('myService', ['$http', function($http){
        function postMethod(username, password){
            var request = {
                method:'POST',
                url: 'https://myurl.com/j_spring_security_check ',
                data: {
                    username:username,
                    password:password
                }
            }
            return $http(request);
        }
    
        return {
            postMethod:postMethod
        }
    ]);
    

    Then you can use your service as following :

    angular.module('myapp').controller('MainCtrl', ['myService', function(myService){
        myService.postMethod('foo', 'bar').then(function(response){
            // Do a UI change ?
        });
    }]);