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.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 {
    return response;
  • 2
    You mentioned that SAP person supplied username and password, I don't see that in your code above. Are you passing the user credentials to connect to the API or is it that you just haven't mentioned here? – Jayant Das Jun 11 '18 at 14:35
  • Hi @JayantDas The SAP person has given the password . But I dont know how to pass the credential – Prasanth Jun 12 '18 at 9:39
  • Use named credentials to store your credentials to SAP and the pass the name into setEndpoint method like this developer.salesforce.com/docs/atlas.en-us.apexcode.meta/… – sfdx bomb Nov 27 '19 at 11:27

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.

|improve this answer|||||

What you need to do here is to pass the Username and Password while making the callout to your SAP services to be able to connect to the services and get appropriate response.

You will need to verify with your SAP team as how they expect the authorization. One of the ways typically used with a Basic authorization and sending it in header is as below:

Blob authHeaderValue = Blob.valueOf(username+':'+password);
String authHeader = 'Basic ' + EncodingUtil.base64Encode(authHeaderValue);
req.setHeader('Authorization', authHeader);
|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.