+ Start a Discussion
Bobby KnezevicBobby Knezevic 

Making an HTTP request to a Salesforce resource within a custom REST endpoint.

I'm struggling to understand what kind of restrictions are in place when making a HTTP request to a Salesforce resource within a custom REST endpoint. When I construct the request in a console as such ...
HttpRequest request = new HttpRequest();
request.setEndpoint('https://<my hostname>/apex/MyVisualforcePage');
request.setMethod('GET');
request.setHeader('Authorization', 'Bearer ' + UserInfo.getSessionId());
request.setBody('param1=foo&param2=bar');

HttpResponse response = new Http().send(request);
System.debug(response.getBody());
... I get the page content in HTML correctly.

However, when I make the same call in a custom REST endpoint I get the following output:
\r\n<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">\n<html>\n<head>\n    <meta HTTP-EQUIV=\"PRAGMA\" CONTENT=\"NO-CACHE\">\n\n\n\n\n\n<script>\nif (this.SfdcApp && this.SfdcApp.projectOneNavigator) { SfdcApp.projectOneNavigator.handleRedirect('https://evestment--dev.cs13.my.salesforce.com?ec=302&startURL=%2Fapex%2FInvoiceNotificationEmail'); }  else \nif (window.location.replace){ \nwindow.location.replace('https://evestment--dev.cs13.my.salesforce.com?ec=302&startURL=%2Fapex%2FInvoiceNotificationEmail');\n} else {;\nwindow.location.href ='https://evestment--dev.cs13.my.salesforce.com?ec=302&startURL=%2Fapex%2FInvoiceNotificationEmail';\n} \n</script>\n\n</head>\n\n\n</html>\n\n\n\n\n\n<!-- Body events -->\n<script type=\"text/javascript\">function bodyOnLoad(){if(window.PreferenceBits){window.PreferenceBits.prototype.csrfToken=\"null\";};}function bodyOnBeforeUnload(){}function bodyOnFocus(){}function bodyOnUnload(){}</script>\n\t\t\t\n</body>\n</html>\n\n\n<!--\n...................................................................................................\n...................................................................................................\n...................................................................................................\n...................................................................................................\n-->\n\n\n

The response appears to be a page that is redirecting to a login page, but I could be wrong. I'm making the call to the custom REST endpoint via Postman when I received the undesirable output above. I've tried obtaining a session id via Postman and using the same session id in the call to the REST endpoint AND the call to the Salesforce resource (i.e. replacing UserInfo.getSessionId() with the session id). The user authenticating is a sys admin and is the same user running the anonymous code in the console for the positive test.

Can anyone shed any light on why this might be happening?
Bobby KnezevicBobby Knezevic
I found a similar question on Stack Exchange: http://salesforce.stackexchange.com/questions/48139/http-callout-not-giving-correct-response-when-calling-fom-visualforce-page
VineetKumarVineetKumar
Look for Connected Apps in salesforce.