+ Start a Discussion
sony sonusony sonu 

CANT MAKE A REST CALLOUT FORM SALESFORCE


All i need to get response  from sap application through the rest callouts . When i tried to use the Chrome APP 'Advanced Rest Client' and  postman have passed the appropriate URL and Content  with  GET AND POST method I was able to retrieve the values from local server database.
For EG : If i pass request  92126 then i was able to get response  'SAN DIEGO' which is correct.

Here is the link (https://chrome.google.com/webstore/detail/advanced-rest-client/hgmloofddffdnphfgcellkdfbfbjeloo?hl=en-US)for Advanced REST Client.

1)i had created a remotesite setting 
2) When i created this REST class in SAlesforce and tried invoking the End Point then its throwing this error.

System.HttpResponse[Status=Service Unavailable, StatusCode=503]
# as the web api url  which is provided to us is in local sql  server i.e  hosted in private,  as we know  THAT salesforce for making callouts the urls must be EXPOSED  in public... bt the url is in private only for the security reasons not hosted in public..we should achieve iT COMPULSARY THERE WILL A ANY SOLUTION FOR ALL THE QUESTIONS   .. IS any way is there to acheive it ??  ..what changeS  should be done in salesforce or LOCAL server to communicate to eachother ..and allows to make the callout ???
Best Answer chosen by sony sonu
pconpcon
Unless you make your server publically accessible on the internet, there is no way for Salesforce to make any REST calls in.  This does not mean that you cannot secure your endpoint, simply that it must be publically routable.  The simpliest way to secure it would be to use something like basic auth.  Just because your chrome application works does not mean that Salesforce will.  Since Salesforce Apex REST calls come from their servers and not from the client's context.

If the flow for this is 100% client side (ie in a Visualforce page that interacts with your web application) you could use Javascript to make the REST call on the client side and as long as the client has network access (is on the same network or VPN) then your calls would work.

All Answers

pconpcon
Unless you make your server publically accessible on the internet, there is no way for Salesforce to make any REST calls in.  This does not mean that you cannot secure your endpoint, simply that it must be publically routable.  The simpliest way to secure it would be to use something like basic auth.  Just because your chrome application works does not mean that Salesforce will.  Since Salesforce Apex REST calls come from their servers and not from the client's context.

If the flow for this is 100% client side (ie in a Visualforce page that interacts with your web application) you could use Javascript to make the REST call on the client side and as long as the client has network access (is on the same network or VPN) then your calls would work.
This was selected as the best answer
sony sonusony sonu
hi pcon,, thanks for your help... i will strt working by using the javascript..can i get your mailid or skype id please ...thanks in advance
pconpcon
Sorry, I'm not going to give that out.  I will gladly help you via the developer forums.  If you have any specific questions, please ask them here and I will do my best to help.

NOTE: When adding code please use the "Add a code sample" button (icon <>) to increase readability and make it easier to reference.
sony sonusony sonu
hi pcon, thanks for your help.,

 
<apex:page> 
<script src="https://code.jquery.com/jquery-2.2.4.min.js" integrity="sha256-BbhdlvQf/xTY9gja0Dq3HiwQF8LaCRTXxZKRutelT44=" crossorigin="anonymous"></script>
<script>
function Callout() { 
var JSONResponse = ''; 
var JsonBody = ''; 
JsonBody = '{{"capital": "delhi"}}'; 
console.log('--JsonBody ------->'+JsonBody );
var xhr = new XMLHttpRequest(); 
xhr.open("POST", "http://xxxxxendpointurlxxxxxx", true); 
<!-- xhr.setRequestHeader("Authorization","Bearer {!AccessToken}" ); --> 
xhr.setRequestHeader("Content-type", "application/json;charset=UTF-8"); 
xhr.setRequestHeader("Accept", "application/json");
xhr.onload = function () { 
JSONResponse = xhr.responseText;
console.log('--JSONResponse ------->'+xhr.responseText); };
xhr.send(JsonBody); 
setTimeout( function() {
if(xhr.status == 404){ 
} else if(xhr.status == 401){ 
} else if(xhr.status == 500){ 
} else if(xhr.status == 200) { 
obj1 = JSON.parse(JSONResponse ); 
for(var x=0;x< obj1.length;x++) { 
var singleEle = obj1[x];
console.log('--singleEle ------->'+singleEle );
} } }, 2000);
} </script>
<apex:form>
<apex:pageBlock>
<apex:pageBlockButtons location="Top">
<button type="button" onclick="Callout();" style="height:22px; width:150px" > Make Callout </button> </apex:pageBlockButtons> </apex:pageBlock> 
</apex:form>
</apex:page>
iam getting this error :
Failed to load https://www.xxxxxxxxxxx...: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'https://.ap5.visual.force.com' is therefore not allowed access. The response had HTTP status code 404
pconpcon
CORS errors [1] are going to be something you will have to address on your server-side.  How you handle it will vary greatly depending on how the back end service is written.

[1] https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS