Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Simple VF page that demonstrates how to hit REST API. NOTE: User must have access to Chatter REST API for this exact demo to work.
<apex:page docType="html-5.0" sidebar="false" showHeader="false"
standardStylesheets="false" cache="true">
<apex:variable var="PARTNER_URL" value="{!$Api.Partner_Server_URL_290}" />
<apex:variable var="PARTNER_URL_SPLIT" value="{!
<apex:variable var="POD" value="{!
<apex:variable var="SESSION_ID" value="{!$Api.Session_ID}"/>
<apex:variable var="SERVER_URL" value="{!'https://' & POD & ''}"/>
<html lang="en">
<meta charset="utf-8"/>
<script src=""></script>
<script type="text/javascript">
var PATH = 'services/data/v26.0/chatter/users/me';
cache: false,
url: '/services/proxy',
beforeSend: function(xhr) {
xhr.setRequestHeader('SalesforceProxy-Endpoint', '{!SERVER_URL}' + PATH);
xhr.setRequestHeader('Authorization', 'OAuth ' + '{!SESSION_ID}');
success: function(data,status,xhr){
$('body').html('Done! You are <b>' +
+ '</b>, and I, this humble page, retrieved your name from the REST API.');
error: function(xhr,status,errorThrown){
$('body').html('Oh no! Got this error: <br/><b style="color:red">'
+ xhr.status + ' ' + errorThrown + ' ' + xhr.responseText + '</b>');
Copy link

sohalloran commented Jan 31, 2013

Hi Richard,
I'm finding that to get it to work on a MyDomain enabled org, you need to strip the MyDomain name from the SERVER_URL.
I created a forked version that does it, but I'm sure there's a cleaner way to do it.

Copy link

CloudDevEnv commented Jul 13, 2015

Very Nice 😄 Because of this I was able to call REST API from Angular.

Copy link

javimoya commented Feb 29, 2016

@niket123 please could you share an example to call REST API from Angular?

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