0

Im trying to connect to the REST API of FreeNAS (http://api.freenas.org/authentication.html) within my AngularJS app. The API uses basic authentication with username and password.

In python this is a very easy thing as there is only one line of code:

requests.get('http://freenas.mydomain/api/v1.0/account/bsdusers/',auth=('root', 'freenas'))

I tried to find something for AngularJS but stumbled only over excrutiating code, e.g. How do I get basic auth working in angularjs?

Is there anything available like this:

$http({
  method: 'GET',
  url: 'http://freenas.mydomain/api/v1.0/account/bsdusers/',
  auth: ['username':'root', 'password':'pw']
}).then(function successCallback(response) {
    // this callback will be called asynchronously
    // when the response is available
  }, function errorCallback(response) {
    // called asynchronously if an error occurs
    // or server returns response with an error status.
  });

2 Answers 2

1

You need to create a function for encoding the user and password in Base64("username:password") and add Authorization header.

You can try encoding your username and password over here https://www.base64encode.org/ and see if it works. "root:freenas" being cm9vdDpmcmVlbmFz you can try the code below.

$http.defaults.headers.common['Authorization'] = 'Basic cm9vdDpmcmVlbmFz';

Once you get it working get implement the Base64 factory you posted ( How do I get basic auth working in angularjs? )

Hope it helps :)

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

Comments

0

You can try like this.

$http.defaults.headers.common = {"Access-Control-Request-Headers": "accept, origin, authorization"}; 
$http.defaults.headers.common['Authorization'] = 'Basic ' + Base64.encode('root' + ':' + 'freenas');
$http({
  method: 'GET',
  url: 'http://freenas.mydomain/api/v1.0/account/bsdusers/'
}).then(function successCallback(response) {
    // this callback will be called asynchronously
    // when the response is available
  }, function errorCallback(response) {
    // called asynchronously if an error occurs
    // or server returns response with an error status.
  });

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.