Skip to content

Instantly share code, notes, and snippets.

@fayefaye02
Forked from richardvanhook/SimpleProxyClient.page
Created October 20, 2015 22:47
Show Gist options
  • Save fayefaye02/42ed9dff9ab2b965ccdc to your computer and use it in GitHub Desktop.
Save fayefaye02/42ed9dff9ab2b965ccdc to your computer and use it in GitHub Desktop.
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">
<!-- TRANSFORM PARTNER URL INTO TRUE PROXY URL -->
<apex:variable var="PARTNER_URL" value="{!$Api.Partner_Server_URL_290}" />
<apex:variable var="PARTNER_URL_SPLIT" value="{!
LEFT(PARTNER_URL,FIND('.visual.force.com',PARTNER_URL)-1)
}"/>
<apex:variable var="POD" value="{!
MID(PARTNER_URL_SPLIT,FIND('.',PARTNER_URL_SPLIT)+1,LEN(PARTNER_URL_SPLIT))
}"/>
<apex:variable var="SESSION_ID" value="{!$Api.Session_ID}"/>
<apex:variable var="SERVER_URL" value="{!'https://' & POD & '.salesforce.com/'}"/>
<html lang="en">
<head>
<meta charset="utf-8"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script>
<script type="text/javascript">
var PATH = 'services/data/v26.0/chatter/users/me';
$(function(){
$.ajax({
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>' + data.name
+ '</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>');
}
});
});
</script>
</head>
<body>Loading...</body>
</html>
</apex:page>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment