I am writing a web service for which sends a SOAP payload as its body. In order to access the 3rd party server, I need an authenticated user. I am using Basic Http authentication for that purpose. Following is the code snippet.

        HTTP auth = new HTTP();
        HTTPRequest r = new HTTPRequest();
        string endpointurl='https://domainname.net/enterprise/soap?ServiceName';
        Blob headerValue = Blob.valueOf(username +':' +password);
        String authorizationHeader = 'Basic ' + 'EncodingUtil.base64Encode(headerValue);
        //String authorizationHeader = username+':'+password;
        system.debug('Encoder Value' + EncodingUtil.base64Encode(headerValue));
        r.setHeader('Authorization', authorizationHeader);
        r.setHeader('SOAPAction', 'http://www.domainname.com/ws/integration/toolkit/2011/05/management#submitDocument');

Following code will post the http request.

            HTTPResponse authresp = auth.send(r);
            if(authresp.getStatusCode() == 200)
                system.debug('Authentication success!!!' + authresp);
            {system.debug('Authentication failed!!!' + authresp + authresp.getStatusCode());}    
        }catch(exception e){}

When I post the request, it is giving an http 500 internal server error. Is the format of sending http request correct? Am I missing some parameters?

  • Have your added the remote site ? check > Your Name | Setup | Security Controls | Remote Site Settings. Commented Apr 24, 2013 at 22:22
  • Is the 500 error generated by Salesforce or by your SOAP endpoint? What are the Content-Type and POST body looking like? Commented Apr 24, 2013 at 23:19
  • The syntax looks right - I know with Twitter, you also have to add something in the body - is there something like that you also need to add e.g req.setBody('grant_type=client_credentials'); Commented Apr 24, 2013 at 23:25
  • @Martin Borthiry: Yes I have added remote site under security settting.
    – Yash Mehta
    Commented Apr 25, 2013 at 14:25
  • 1
    @user320: I was missing content type in the header. Now I am getting response code 200.
    – Yash Mehta
    Commented Apr 25, 2013 at 14:26

1 Answer 1


If the 500 error is coming from your SOAP service (and not Salesforce), check:

  1. what your Content-Type HTTP header looks like,

  2. the body is set in order for the Content-Length header to populate,

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .