9

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';
        r.setEndpoint(endpointurl);
        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');
        r.setMethod('POST');

Following code will post the http request.

      try
        {
            HTTPResponse authresp = auth.send(r);
            if(authresp.getStatusCode() == 200)
            { 
                system.debug('Authentication success!!!' + authresp);
            }
            else
            {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. – Martin Borthiry Apr 24 '13 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? – bigassforce Apr 24 '13 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'); – BritishBoyinDC Apr 24 '13 at 23:25
  • @Martin Borthiry: Yes I have added remote site under security settting. – Yash Mehta Apr 25 '13 at 14:25
  • 1
    @user320: I was missing content type in the header. Now I am getting response code 200. – Yash Mehta Apr 25 '13 at 14:26
5

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,

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.