How to get authenticated Json object in angularjs - angularjs

I want to get an object by using get request in angularJs. Server asks for authentication details: how to pass authentication details?
$http({
method: 'GET',
url: 'http://192.168.1.133:8080/xyz-api/api/customer/token/xyzname',
headers: {
'Content-Type': 'application/JSON'
}
}).success(function (data) {
$scope.customerDetails = data;
$scope.CUSTOMER_ID = $scope.customerDetails.id;
$scope.companyName = $scope.customerDetails.name;
}).error(function () {
alert('error');
});

Related

null with receipt data in angular $http

This is my code:
$scope.doLogin = function() {
$http({
method: "POST",
url: "http://localhost:8000/api/v1/user/user_signin",
data: { username: $scope.user.username, password: $scope.user.password },
headers: { 'Content-Type': 'application/json' },
responseType:'json'
}).success(function (data) {
console.log('data success');
console.log(JSON.stringify(data));
})
.error(function(data, status, headers,config){
console.log('data error');
});
};
Consol says:
data success
null
What I do wrong?
P.S.: API request is correct - I checked it in postman.
You set responseType to 'json' make sure that your server side is also sending the response compatible to your format 'json'.
Or simply remove the response type check if it works

Paypal API with angularjs

I'm making a PayPal API call in the context of an e-Commerce solution I'm developing in Angular-JS. I was able to get an auth_token back from PayPal using the following code.
my code :
var basicAuthString = btoa('AUAlrIhHNoiQlmaW.............:EJk......');
$http({
url: 'https://api.sandbox.paypal.com/v1/oauth2/token',
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/x-www-form-urlencoded',
'Authorization': 'Basic ' + basicAuthString,
},
data: { 'grant_type': 'client_credentials' }
}).success(function (data, status, headers, config) {
console.log(data)
}).error(function (error) {
console.log("erreur "+error);
});
When sending the data , I receiving an error
error:{"error":"unsupported_grant_type","error_description":"Grant Type is NULL"}
You should send the grant type like this:
data: 'grant_type=client_credentials'

HttpResponseMessage from WebAPI from angular http post call

I have a UI project , which is an Angular JS project and Web API project and i am new to Angular. I am calling a login method of API controller which does the DB check and its sending OK message. But its going to error part of Angular http promise call. What can be the possible reasons? This is the API Call
function AutenticateUser(input) {
var deferred = $q.defer();
$http({
method: 'POST',
data: input,
url: config.serviceUrl + config.loginUrl,
transformRequest: function (input) {
var str = [];
for (var p in input)
str.push(encodeURIComponent(p) + "=" + encodeURIComponent(input[p]));
return str.join("&");
},
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Accept': 'application/json'
}
}).then(function (result) {
userInfo = {
accessToken: result.data.access_token,
userName: input.username
};
}, function (error) {
deferred.reject(error);
});
return deferred.promise;
}
Does the accept header has to do anything with it?

angularjs post request to nodejs api

I've angularjs post call (submit a login form data) to a /login nodejs API endpoint. The data received at Nodejs endpoint (in request.body) is not in json format but it has extra padding as shown below,
{ '{"email": "a#b.com", "password": "aaa"}': ''}
What is this format? How do I access 'email' and/or password from this object?
Client code,
login: function(loginData, callback) {
$http({
method: 'POST',
url: '/api/login',
data: loginData,
headers: {'Content-Type': 'application/x-www.form-urlencoded'}
}).then(function successCallback(response) {
}, function errorCallback(response) {
});
}
Server code:
app.post('/login', function(req, res) {
console.log('Email:' + req.body.email); //this gives undefined error
console.log(req.body); // shows { '{"email": "a#b.com", "password": "aaa"}': ''}
}
What am I missing? Any help is appreciated.
--Atarangp
By default angularjs use JSON.stringify. If you wanna use x-www-form-urlencoded, you have to specify your transform function.
// transforme obj = {attr1: val1} to "attr1=" + encodeURIComponent(val1) + "&attr2=" ...
function transformRequestToUrlEncoded(obj) {
var str = [];
for(var p in obj)
str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p]));
return str.join("&");
}
$http({
method: 'POST',
url: your_url,
headers: {'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'},
transformRequest: transformRequestToUrlEncoded, // specify the transforme function
data: datas
});

ASP.NET MVC Web API Primitive Post not binding Angular

I have the following Action
[HttpPost]
[Route("api/client/log")]
public void SetClientLog([FromBody]string error)
{
ClientLogger.LogError(error);
}
And in angular i'm trying to post the following where log is just a string
$http({
method: 'POST',
url: "api/client/log",
data: log,
headers: {'Content-Type': 'application/json'}
});
Make sure you prefix your value with "=" and set the correct Content-Type:
$http.post('api/Companies/Search', "="+$scope.query, {
headers: { 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8' }
})
.success(function (data, status) {
$scope.result = data;
})
.error(function (data, status) {
$scope.data = data || "Request failed";
$scope.status = status;
});

Resources