2

I have this curl command that I would like to simulate with angular:

curl -k -F [email protected] -u username:Password url

At the moment I went about doing an angular post. However, I run into the problem of authentication. There is no parameter for me to put the user id and password.

Angular code:

   $scope.postCall = function () {
        $scope.ngResult = "clicked";

        var paramsJson = {
            "imessageIdT": $scope.messageIdT,
            "ilobT": $scope.lobT,
            "iregionIdT": $scope.regionIdT,
            "iassetClassT": $scope.assetClassT,
            "additionalInfoT": $scope.additionalInfoT

        };

      var config = {
        paramsJson: paramsJson
      };

      $http.post("WEBSITE", paramsJson, config)
        .success(function (data, status, headers, config)
        {
          $scope.ngResult = logResult("POST SUCCESS", data, status, headers, config);
          //$scope.ngResult = "Yes";
        })
        .error(function (data, status, headers, config)
        {
          $scope.ngResult = logResult("POST ERROR", data, status, headers, config);

          //$scope.ngResult = "No";
        });


    };  
3
  • possible duplicate of How to use Curl in javascript Commented Aug 13, 2015 at 14:02
  • @SergiuParaschiv is there no implementation in angular? Commented Aug 13, 2015 at 14:04
  • 1
    Why would there be one? AngularJS is a JavaScript framework. Anything written in JavaScript for the browser will work. This task is as much AngularJS related as it is Backbone or NodeJS. The only AngularJS thing you need to do after using the library in that answer is to $scope.$apply when you get the response, just like $http does. Commented Aug 13, 2015 at 14:06

2 Answers 2

2

Assuming basic authentication, not tested, this might work:

var username = "...", password = "***";
var config = {
    headers: {
        Authorization: "Basic " + window.btoa(username+":"+password)
    },
    method: "get", // or "post",
    url: "destination.com"
};

$http(config).success(function(){
    // on success
}).error(function(){
    // on failure
});

The only thing I'm not certain about is window.btoa, if it's an RFC2045-MIME compliant variant of Base64, then you're good.

But my example is an over-simplification. Essentially, you should determine the authentication scheme supported by the server. It could be any one the following specified by IANA:

  • Basic
  • Bearer
  • Digest
  • HOBA
  • Negotiate
  • OAuth

Depending on the required scheme, you should compose the request header accordingly.

Sign up to request clarification or add additional context in comments.

3 Comments

would you also put paramsJson:paramsJson inside config?
It would help you if you had a look at the angular documentation of $http, the API does not make any mention of a property named paramJson. If you're making a GET request consider specifying the request data by use of the param property. But if you're making a POST request, then the data property would be more appropriate. What you probable want is something like this: config = { /* ... */ param: {name: "some-name", age: 44 }};.
The above answer by @IgweKalu is the correct answer. To make my AngularJS call a bit more friendly though I did the var userPass = window.btoa(username":"password) out side promise and just based that var to: { ... Authorization: 'Basic + userPass'}
1

This depends on the api you are connecting to. Usually you would log and the server will return you an authentication token on the headers of the response. 1 Basic auth Authorization: Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ== 2 Aoth2 Authorization: Bearer mF_9.B5f-4.1JqM

So you will need to add this header to your request:

 $http.post("WEBSITE", paramsJson, angular.extend({}, config, {headers: {
    'Authorization': token}}))

If the request is to another domain you should use jsonp.

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.