[SalesForce] Callout to SAP from Salesforce using REST API

I am trying to pass a json data to an external URL. With a basic knowledge, I have added them to remote site settings. The sample URL is http://xyz-xyz.sap.xx.com:50000/xyz/xyz. I have created an apex class and tried the callout from Apex.

But I'm getting this error:

"Status=Service Unavailable, StatusCode=503"

But the SAP person tried this from SOAPUI with basic authentication by suppling the username and password and got the status as 202.
Could someone please tell me where I'm making the mistake. From Salesforce we were not able to hit the SAP endpoint itself.

  public static HttpResponse makePostCallout() {
    Http http = new Http();
    HttpRequest request = new HttpRequest();
    request.setEndpoint('http://xyz-xyz.sap.xx.com:50000/xyz/xyz');
    system.debug('endPoint+++++'+request.getEndpoint());

    request.setMethod('POST');
    request.setHeader('Content-Type', 'application/json;charset=UTF-8');
    request.setBody('{"Id" :"123", "name":"mighty moose"}');
    HttpResponse response = http.send(request);
    // Parse the JSON response
    if (response.getStatusCode() != 201) {
        System.debug('The status code returned was not expected: ' +
            response.getStatusCode() + ' ' + response.getStatus());
    } else {
        System.debug(response.getBody());
    }
    return response;
} 

Best Answer

There are a number of ways to connect to another service where you're passing user credentials. Some are much more secure than others. You may want to ask your counterpart what signature he used to pass the credentials. One way is to pass them in your HttpResponse Method is by using setHeader. You'd use it twice, once for the username and again for the password.

For more on this I highly recommend you go through the Trailhead Module covering Apex Integration Services.

Related Topic