function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ 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.
Satishakumar AwatiSatishakumar Awati
I am facing the same issue, under what circumstances this page is returned by salesforce?
Shanny LubowitzShanny Lubowitz
HTTP AcceptIndicates the format that your client accepts for the response body. Possible values are application/json and application/xml. The default value is application/json.HTTP Content-typeIndicates the format of the request body that you attach to the request. Possible values are application/json and application/xml.

myccpay (https://www.myccpay.run/)
Suraj Tripathi 47Suraj Tripathi 47

Hi bobby,
I think you have to use pageReference for Vfpage calling .
First you have to call http request and get response in another org and after that use pageReference and set Param like this.

PageReference pageRef = new PageReference('/apex/myVfPage');
pageRef.setRedirect(true);
pageRef.getParameters().put('param1', foo);

So if you find your Solution then give it Best answer.

Thank you!
Regards,

Suraj Tripathi 

 

Rowshan AraRowshan Ara
I came to your website after traveling for a long time.I was looking for a site like this.Apni jodi cheleder Islamic Name (http://www.ajkerfact.com/2022/04/cheleder-islamic-name-bangla.html) Kuja Thaken Tahola..
Rowshan AraRowshan Ara
The Saleforce REST API is more than a set of default routes. This is a tool for creating custom routes and endpoints. WordPress provides a default set of front-end URLs https://recentfacts.com/