1
Global class TestCmis{   
        Webservice static void getCmis( Id LeadId){        
            system.debug('HAnana---->');
            HttpRequest req = new HttpRequest();
            req.setEndpoint('http://127.0.0.1/fncmis'); // masked IP for this example
            req.setMethod('GET'); 
            String username = 'XXXXXX';
            String password = 'XXXXXX';
            Blob headerValue = Blob.valueOf(username + ':' + password);
            String authorizationHeader = 'BASIC ' + EncodingUtil.base64Encode(headerValue);
            req.setHeader('Authorization', authorizationHeader);
            //req.setHeader('Content-Type','application/json');
            system.debug('Auth---->');
            Http h = new Http();
            HttpResponse res = h.send(req); 
            res.setHeader('Content-Type', 'application/json');
            //return res.getbody();
            system.debug('res---->'+res);
            system.debug('req---->'+req);  
            system.debug('res.getBody()---->'+res.getBody());
        }
}

This webservice works fine when testing it through SOAP UI tool. It fails only in Salesforce.

1

401 means that there's some authentication that the web service is requiring that you're not complying with. It has nothing to do with Salesforce.

See Wikipedia on 401:

401 Unauthorized (RFC 7235)

Similar to 403 Forbidden, but specifically for use when authentication is required and has failed or has not yet been provided. The response must include a WWW-Authenticate header field containing a challenge applicable to the requested resource. See Basic access authentication and Digest access authentication.

On the other hand, you probably shouldn't use Salesforce tests to test external web services. Use Salesforce tests to test your code.

  • I am getting proper response through soap UI and restclient. It means we don't need to add anything else right ? Are you taking about my testcmis class name ? – HIMANSHU ATAL Jul 6 '15 at 15:24
  • @HIMANSHUATAL I don't understand your comment at all. I don't know whether you need to add something else or not. And I have not referenced your class name at all. – Charles Koppelman Jul 6 '15 at 15:57
  • Sorry for the confusion. Please let me know what should I make changes according to you. We don't have any other credentials other than username and password for the endpoint url. Do I need to add any annotation ? – HIMANSHU ATAL Jul 6 '15 at 17:43
  • @HIMANSHUATAL It seems like you're asking for critique of a unit test against an external web service. According to me, one does not unit test external web services within the same suite of tests one runs on one's own code. So the change you should make is to remove this test from your test suite. – Charles Koppelman Jul 7 '15 at 16:34
1

You are using authorisation as

BASIC

but it has to be

Basic

Try changing it and it will work.

  • That I did later... but eventhough same error. – HIMANSHU ATAL Jul 7 '15 at 3:10

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.