Skip to content

Instantly share code, notes, and snippets.

@rrcook
Forked from metadaddy/TestRestApi.page
Last active March 23, 2018 04:38
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save rrcook/0f07838bc3d35d017206 to your computer and use it in GitHub Desktop.
Save rrcook/0f07838bc3d35d017206 to your computer and use it in GitHub Desktop.
<apex:page docType="html-5.0" >
<html ng-app="restApiApp" >
<head>
<apex:includeScript value="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.16/angular.min.js"/>
</head>
<body ng-controller="MainCtrl">
<h1>Test REST API from JavaScript with AngularJS</h1>
<div ng-init="sessionId='{!$Api.Session_ID}'; url='/services/data/v30.0/query?q=SELECT+Name+FROM+Account+LIMIT+10'">
<input id="query" size="120" value="{{url}}" ng-model="url"/>
<button ng-click="submit()">Submit</button>
<p>Results:</p>
<pre>{{results | json}}</pre>
</div>
</body>
<script>
angular.module('restApiApp', []).controller('MainCtrl', function($scope, $http) {
$scope.results = ""; // sessionId, url from ng-init
$scope.submit = function() {
$http.defaults.headers.common.Authorization = 'Bearer ' + $scope.sessionId;
$http.get($scope.url).then(function(response) {
$scope.results = response.data;
}, function(errResponse) {
console.log('Error while fetching data');
});
};
});
</script>
</html>
</apex:page>
@rrcook
Copy link
Author

rrcook commented Jul 14, 2014

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment