Can we hit a private server URL with HTTP request from Salesforce ?? or the server should always be public.
I have make the Post callout which was written in Rails (hosted on Private server- VPN connected) which will get the Json response. When i tried to execute the code in Salesforce, i got an error 'Server unavailable,status code = 503'.
Below is the Apex class and Response Code
public class CreateSalesOrder {
public static String makePostCallout() {
String responsebody;
JSONGenerator gen = JSON.createGenerator(true);
gen.writeStartObject();
gen.writeStringField('companyName', '1234');
gen.writeStringField('firstName', 'open');
gen.writeStringField('lastName', 'High');
gen.writeStringField('address1', 'High');
gen.writeEndObject();
String jsonS = gen.getAsString();
// Sending the http body with JSON
String endpoint = 'http://xxx.stage.com/salesforce/api';
HttpRequest req = new HttpRequest();
req.setEndpoint(endpoint);
req.setMethod('POST');
req.setHeader('Content-Type', 'application/json;charset=UTF-8');
req.setHeader('Accept', 'text/html');
req.setbody(jsonS);
System.debug(req);
try {
Http http = new Http();
HTTPResponse response = http.send(req);
System.debug('HTTP Response Code: ' + response.getStatusCode());
System.debug('Response Body: ' + response.getBody());
return response.getBody();
} catch (Exception e) {
System.debug('Callout Error:' + e.getMessage());
return e.getMessage();
}
}
}
Best Answer
Your server hosting the service will need to be able to respond to the public IP addresses that Salesforce uses to call it from Apex. Otherwise there would be no way for Salesforce to communicate with it.
You can see the list of IP addresses in - What are the Salesforce IP Addresses & Domains to whitelist?
I see you have the endpoint of
http://xxx.stage.com/salesforce/api
. Salesforce will need to be able to resolvexxx.stage.com
to the public IP address of the target server.