0

I am a newbie and am writing a SOAP web service (for integration purpose), in order to execute the SOAP call I need to authenticate the user first(standard integration user).

Following is the code snippet for it. However, when I execute the callout, it throws error code 500 for Basic Http request and error code 401 for the second Http request.

Is this the correct approach?

HTTP auth = new HTTP();
HTTPRequest r = new HTTPRequest();
r.setEndpoint('https://domainname.net/enterprise/soap?ServiceName=IntegrationManagementService');
Blob headerValue = Blob.valueOf(username+':'+password);
String authorizationHeader = 'Basic ' + EncodingUtil.base64Encode(headerValue);
r.setHeader('Authorization', authorizationHeader);
r.setMethod('POST');

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){}

   //construct http request
    string endpointURL = 'https://doaminname.net/enterprise/soap?ServiceName=IntegrationManagementService';
   HttpRequest req = new HttpRequest();
   req.setMethod('POST');

   req.setEndpoint(endpointURL);
   req.setHeader('Content-Type','application/xml');
   req.setBody(TaleoXML);

   //send http request
   Http http = new Http();
   try
   {
       HttpResponse res = http.send(req);

       //check the response
       if(res.getStatusCode() == 200)
       { 
           system.debug('Callout success!!!' + res);
       }
       else
       {system.debug('Callout failed!!!' + res + res.getStatusCode());}    
   }catch(exception e){}  
Stevoisiak
  • 23,794
  • 27
  • 122
  • 225
Yash Mehta
  • 101
  • 1
  • 2

2 Answers2

0

I'm not familiar with the library you're using here, but I can suggest a few possibilities to investigate:

  • Basic authentication is a stateless method. You don't log in and stay logged in with it. This means there's no separate initial authentication request (as your code implies); you include the Authorization header with every request. That's why you're getting the 401 on the second request.

  • On the first request, you are providing credentials and the the server is encountering an unexpected internal error (that's what 500 means). If it includes a body with the error response, it might have more information. I would guess that it has something to do with the fact that you didn't provide a body with your POST and the server was not expecting that.

If this is the first time you're using SOAP, you would probably be better off using a dedicated SOAP library rather than trying to construct requests yourself.

Rhett Sutphin
  • 1,045
  • 8
  • 15
0

In the second request you don't include Authentication, that's why you get a 401 (Unauthorized) error.

In the first request, it seems you authenticate ok but the server fails to process the request. I think you missed to refer to the function/operation of the IntegrationManagementService webservice that you want to use. Or you are using a function/operation that needs MTOM enabled.

NurDev
  • 11
  • 2