I was trying to callout Share point Service from apex using REST API.Which is having windows authentication which is of type NTLM. But i was getting 401 Unauthorized access.But i was able to access form SOAPUI and from browser.

Http objhttp;
HttpRequest req ;
HttpResponse res ;
objhttp = new Http();
req = new HttpRequest();
res = new HttpResponse();  
req.setEndpoint('my end point url');
Blob headerValue = Blob.valueOf('domain"\"username'+':'+'password');
String authorizationHeader = 'NTLM '+EncodingUtil.base64Encode(headerValue);
system.debug('*** authorizationHeader '+authorizationHeader );
// Set the necessary Headers
req.setHeader('Content-type','application/vnd.cpc.authreturn+xml; charset=UTF-8');
req.setHeader('Accept-Language', 'en-CA');
req.setHeader('Content-Length', '200');
system.debug('*** req is '+req);
try {
res = objhttp.send(req);
System.debug('res values in req: '+ res);
system.debug('*** response is '+response);
catch(System.CalloutException e) {
System.debug('Callout error : '+ e);

This was the code I am using to do call outs.Previously when the authentication is 'basic' i am getting proper response, but now when it changed to windows authentication by using 'NTLM' its giving 401 unauthorized error. But SOAPUI is giving proper response using 'NTLM'. Is there any possibility to call Sharepoint having windows authentication using this NTLM ..

  • Have you figured out how to call an NTLM web service? I'm getting 415 error. Sep 1, 2016 at 18:22
  • @AndrewMuza no i couldn't figured how to call NTML services, we have changed the authentication mechanism on endpoint services
    – Rajasekhar
    Sep 3, 2016 at 6:16
  • 1
    I figured it out. I missed the thing with headers in request. I put header into request first. Then I saw discussion about 415 error. Additional parameter was added to send method of the library. After I passed map of header key/value to the method it started working like a charm. Sep 4, 2016 at 8:12
  • @andriimuzychuk - I am facing the same error. Can you please have a look at this - salesforce.stackexchange.com/questions/219404/…
    – Samir
    May 25, 2018 at 10:09

1 Answer 1


It should first send you a challenge. Does it not even do that?

Have you checked this NTLM implementation in Apex? https://github.com/natewallace/ApexNTLM

  • Thanks for the link Willem, its not sending any challenge , just giving 401 unauthorized access... but when i access with SOAPUI giving proper result..
    – Rajasekhar
    Sep 17, 2015 at 9:06
  • Are you 100% certain the request body and headers are identical from Apex and SOAPUI? Could you post them here? Sep 17, 2015 at 10:23
  • 1
    That make me really confusing ... i feel the headers are not matching . In SOAP UI i am not passing any headers, just in authentication section i am giving uname,password and domain name. You can see my salesforce headers above. But the diff i observed is SOAP UI encoded authorization code is not matching with salesforce ones in salesforce i am giving as domain/username : password but SOAP UI is taking as uname password and domain.
    – Rajasekhar
    Sep 17, 2015 at 10:38
  • I suggest mark this as an answer with one addition - github.com/natewallace/ApexNTLM/issues/3 Sep 4, 2016 at 8:14

You must log in to answer this question.

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