2

Getting error: Auth String must be valid base64 format...Posting the code...Kindly Help!

String url = 'http://cloud.ocrsdk.com/processImage?language=english&exportformat=pdfSearchable';

String appId='';
String pwd='';
String toEncode=appId+':' + pwd;
Blob beforeblob = Blob.valueOf(toEncode);
string paramvalue = EncodingUtil.base64Encode(beforeblob);
Attachment att = new Attachment();
att = [SELECT ID,Body,ContentType FROM Attachment where ContentType='image/jpeg' and ID='00P2800000KuwQf' LIMIT 1];
String strImg=EncodingUtil.base64Encode(att.body);
Http h = new Http();
        HttpRequest req = new HttpRequest();

        req.setEndpoint(url);
        req.setMethod('POST');
        req.setHeader( 'Authorization', 'Basic' + paramvalue);
        req.setHeader('Content-Type', 'image/jpeg');
        req.setBody(imgBlob);
        req.setBody(strImg);        
        req.setHeader('Content-Length', String.valueOf(strImg.length()));
        HTTPResponse res = h.send(req);
        System.debug(res.getBody());
  • 1
    May not be the cause of this error, but you need a space after the word Basic i.e. 'Basic ' + paramvalue. – Keith C Jan 8 '17 at 16:13
  • Getting this error now...Unsupported image file format. Api supports a jpeg image and attachment contains a jpeg file. – Keyser.soze Jan 8 '17 at 16:27
4

This works for me using ABBY:

String encodedUrl = EncodingUtil.urlDecode(url, 'UTF-8');
String toEncode = appId + ':' + pwd;
String encoded = EncodingUtil.urlEncode(toEncode, 'iso-8859-1');

String key = EncodingUtil.base64Encode(Blob.valueOf(toEncode));

HttpRequest request = new HttpRequest();
request.setEndpoint(encodedUrl);
request.setMethod('POST');
request.setHeader('Authorization', 'Basic ' + key);

Attachment file = [Select Body, Name, ContentType, bodyLength  From Attachment Where ParentId = :this.scannedImage.Id And Name != 'description.xml' LIMIT 1];       
request.setHeader('Content-Type', file.ContentType);
request.setHeader('Content-Length', '' + file.bodyLength );
request.setBodyAsBlob(file.Body);

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.