Ed Chmiel
Posts: 0
Joined: Fri Jun 27, 2014 11:58 pm

REST API database access via AngularJS

I have the following code in an angularjs controller in a standalone project that i need to access my data in an appery database:

Code: Select all

     function GetAll() { 
         var config = { 
             headers: { 
                 'X-Appery-Database-Id' : 'mydatabaseid', 
                 'Accept': 'application/json;odata=verbose' 
             } 
         }; 
     return $http.get('[url=https://api.appery.io/rest/1/db/users]https://api.appery.io/rest/1/db/users[/url]', config).then(handleSuccess, handleError('Error getting all users')); 
     } 

It returns 401 unauthorized. I saw in some other posts about having a session token:
var config = {
headers: {
'X-Appery-Database-Id' : 'mydatabaseid',
'X-Appery-Session-Token' : ''
'Accept': 'application/json;odata=verbose'
}
};

but I have no idea how to create and/or provide one. Can you help? TIA

Serhii Kulibaba
Posts: 150
Joined: Tue Aug 27, 2013 1:47 pm

REST API database access via AngularJS

Hello Ed,

Please use login service for getting the session token: https://devcenter.appery.io/documenta...

Ed Chmiel
Posts: 0
Joined: Fri Jun 27, 2014 11:58 pm

REST API database access via AngularJS

i see, but still having trouble with the format. Get 400 bad request:

Code: Select all

     var config = { 
         headers: { 
             'X-Appery-Database-Id': 'mydatabaseid', 
             'Accept': 'application/json;odata=verbose', 
             'Content-Type': 'application/x-www-form-urlencoded' // Note the appropriate header 
         } 
     }; 
     var thedata = { 
         data: { 
             'username': encodeURIComponent('adminuser'), 
             'password': encodeURIComponent('adminpassword') 
         } 
     }; 
     console.log('login = ' + config + thedata); 
     $http.get('[url=https://api.appery.io/rest/1/db/login]https://api.appery.io/rest/1/db/login[/url]', config, thedata) 
     .then(function (res) { 
             // success getting login means we now have session token 
             sessionToken = res.sessionToken; 
             console.log('session token recvd = ' + sessionToken); 
         }, 

         function (res) { 
             handleError('Error login') 
             console.log(' login error'); 
         });
Serhii Kulibaba
Posts: 150
Joined: Tue Aug 27, 2013 1:47 pm

REST API database access via AngularJS

Please send us full Appery.io server response.

Ed Chmiel
Posts: 0
Joined: Fri Jun 27, 2014 11:58 pm

REST API database access via AngularJS

I am now trying this one:
$http({
method: 'GET',
url:
'https://api.appery.io/rest/1/db/login',
headers: {
'X-Appery-Database-Id': 'xxxxxxxxxx'
},
transformRequest: function (data) {
var postData = [];
for (var prop in data) {
postData.push(encodeURIComponent(prop) + "=" + encodeURIComponent(data[prop]));
console.log('encoding ' + prop + ' ' + data[prop]);
}
return postData.join("&");
},
data: { username: 'xxxx, password: 'xxxx' }
})

and still get 400 bad request - "code":"DBUI106","description":"User name must be present."

Serhii Kulibaba
Posts: 150
Joined: Tue Aug 27, 2013 1:47 pm

REST API database access via AngularJS

Ed,

Please use JS below:

pre$.ajax({
method: 'GET',
url: 'https://api.appery.io/rest/1/db/login',
headers: {
'X-Appery-Database-Id': '516fb7c3e4b0e0d8f16ccf9a'
},
data: {
username: 'test', password: 'test' },
success: function(response){
console.log("sessionToken=" + response.sessionToken);
}
});/pre

Return to “Issues”