You need to sign in to do that
Don't have an account?
Narender Singh(Nads)
Getting 'Internal Server Error'(Status Code 500) as response when making a callout to Einstein OCR with base64 content
In addition to what query title says, when making callout via URL it seems to be working. Please find the code below:
EinsteinOCRPredictionHandler.cls
EinsteinOCRPredictionHandler.cls
public class EinsteinOCRPredictionHandler { final static String VISION_API = 'https://api.einstein.ai/v2/vision'; final static String OCREndpoint = VISION_API + '/ocr'; final static String cmdtDeveloperName = 'Einstein_Vision_API_JWT'; public class EinsteinOCRPredictionHandlerException extends Exception {} public static EinsteinOCRPredictionResponseWrapper predictViaURL(String fileURL, String detectionType, String accessToken, String model) { String response = detectText(fileURL, detectionType, accessToken, model, false); return parseResponse(response); } public static EinsteinOCRPredictionResponseWrapper predictViaBase64(String base64String, String detectionType, String accessToken, String model) { String response = detectText(base64String, detectionType, accessToken, model, true); return parseResponse(response); } public static EinsteinOCRPredictionResponseWrapper predictViaBlob(Blob fileBlob, String detectionType, String accessToken, String model) { String response = detectText(EncodingUtil.base64Encode(fileBlob), detectionType, accessToken, model, true); return parseResponse(response); } public static String detectText(String sample, String detectionType, String accessToken, String model, boolean isBase64) { string contentType = HttpFormBuilder.GetContentType(); // Compose the form string form64 = ''; form64 += HttpFormBuilder.WriteBoundary(); form64 += HttpFormBuilder.WriteBodyParameter('modelId', EncodingUtil.urlEncode(model, 'UTF-8')); form64 += HttpFormBuilder.WriteBoundary(); form64 += HttpFormBuilder.WriteBodyParameter('task', EncodingUtil.urlEncode(detectionType, 'UTF-8')); form64 += HttpFormBuilder.WriteBoundary(); if(isBase64) { form64 += HttpFormBuilder.WriteBodyParameter('sampleBase64Content', sample); } else { form64 += HttpFormBuilder.WriteBodyParameter('sampleLocation', sample); } form64 += HttpFormBuilder.WriteBoundary(HttpFormBuilder.EndingType.CrLf); blob formBlob = EncodingUtil.base64Decode(form64); string contentLength = string.valueOf(formBlob.size()); // Compose the http request HttpRequest httpRequest = new HttpRequest(); httpRequest.setBodyAsBlob(formBlob); httpRequest.setHeader('Connection', 'keep-alive'); httpRequest.setHeader('Content-Length', contentLength); httpRequest.setHeader('Content-Type', contentType); httpRequest.setMethod('POST'); //httpRequest.setTimeout(120000); httpRequest.setHeader('Authorization','Bearer ' + accessToken); httpRequest.setEndpoint(OCREndpoint); Http http = new Http(); HTTPResponse res = http.send(httpRequest); System.debug(res.getStatus()); String responseBody; Integer statusCode = res.getStatusCode(); String requestStatus = res.getStatus(); if ( statusCode == 200) { responseBody = res.getBody(); } else { throw new EinsteinOCRPredictionHandlerException('Callout unsuccessful! Status code: '+statusCode + '. Status: '+requestStatus); } return responseBody; } static EinsteinOCRPredictionResponseWrapper parseResponse(String response) { if(String.isEmpty(response)) { throw new EinsteinOCRPredictionHandlerException('Empty response received'); } EinsteinOCRPredictionResponseWrapper obj = new EinsteinOCRPredictionResponseWrapper(); obj = (EinsteinOCRPredictionResponseWrapper)JSON.deserialize(response, EinsteinOCRPredictionResponseWrapper.class); return obj; } }HttpFormBuilder.cls
public class HttpFormBuilder { // The boundary is alligned so it doesn't produce padding characters when base64 encoded. private final static string Boundary = '1ff13444ed8140c7a32fc4e6451aa76d'; /** * Returns the request's content type for multipart/form-data requests. */ public static string GetContentType() { return 'multipart/form-data; charset="UTF-8"; boundary="' + Boundary + '"'; } /** * Pad the value with spaces until the base64 encoding is no longer padded. */ private static string SafelyPad( string value, string valueCrLf64, string lineBreaks) { string valueCrLf = ''; blob valueCrLfBlob = null; while (valueCrLf64.endsWith('=')) { value += ' '; valueCrLf = value + lineBreaks; valueCrLfBlob = blob.valueOf(valueCrLf); valueCrLf64 = EncodingUtil.base64Encode(valueCrLfBlob); } return valueCrLf64; } /** * Write a boundary between parameters to the form's body. */ public static string WriteBoundary() { string value = '--' + Boundary + '\r\n'; blob valueBlob = blob.valueOf(value); return EncodingUtil.base64Encode(valueBlob); } /** * Write a boundary at the end of the form's body. */ public static string WriteBoundary( EndingType ending) { string value = ''; if (ending == EndingType.Cr) { // The file's base64 was padded with a single '=', // so it was replaced with '\r'. Now we have to // prepend the boundary with '\n' to complete // the line break. value += '\n'; } else if (ending == EndingType.None) { // The file's base64 was not padded at all, // so we have to prepend the boundary with // '\r\n' to create the line break. value += '\r\n'; } // Else: // The file's base64 was padded with a double '=', // so they were replaced with '\r\n'. We don't have to // do anything to the boundary because there's a complete // line break before it. value += '--' + Boundary + '--'; blob valueBlob = blob.valueOf(value); return EncodingUtil.base64Encode(valueBlob); } /** * Write a key-value pair to the form's body. */ public static string WriteBodyParameter( string key, string value) { string contentDisposition = 'Content-Disposition: form-data; name="' + key + '"'; string contentDispositionCrLf = contentDisposition + '\r\n\r\n'; blob contentDispositionCrLfBlob = blob.valueOf(contentDispositionCrLf); string contentDispositionCrLf64 = EncodingUtil.base64Encode(contentDispositionCrLfBlob); string content = SafelyPad(contentDisposition, contentDispositionCrLf64, '\r\n\r\n'); string valueCrLf = value + '\r\n'; blob valueCrLfBlob = blob.valueOf(valueCrLf); string valueCrLf64 = EncodingUtil.base64Encode(valueCrLfBlob); content += SafelyPad(value, valueCrLf64, '\r\n'); return content; } /** * Helper enum indicating how a file's base64 padding was replaced. */ public enum EndingType { Cr, CrLf, None } }
So i suggest take a look at the code here
https://github.com/muenzpraeger/salesforce-einstein-platform-apex/blob/master/force-app/main/default/classes/Einstein_HttpBodyPart.cls
Enhance your class HttpFormBuilder class to include the below method
Let me know if you still run into issues with it!!
All Answers
So i suggest take a look at the code here
https://github.com/muenzpraeger/salesforce-einstein-platform-apex/blob/master/force-app/main/default/classes/Einstein_HttpBodyPart.cls
Enhance your class HttpFormBuilder class to include the below method
Let me know if you still run into issues with it!!
It may sound weird but the issue got fixed on it's own, didn't make any changes to my code. 😅
Still marking your answer as best assuming it's a better version of old HttpFormBuilder class. :)