function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
dai tran 6dai tran 6 

How can call google vision api from apex?

I upload a image and use google vision api ocr it:
public blob file { get; set; }

public void uploadImage()
    {
String b64String = EncodingUtil.base64Encode(file);		
            Http http = new Http();	 
            HttpRequest  request = new HttpRequest();
            request.setMethod('POST');	     
            request.setHeader('Content-Length', '2000');
            request.setHeader('Content-Type', 'application/json');             
            request.setEndpoint('https://vision.googleapis.com/v1/images:annotate?key=AIzaSyDY2VbfNW9rIXOJs9O....');	        
                 
            request.setBody(b64String);	        
            HttpResponse response = http.send(request);	        
            imagetext= response.getBody();
           System.debug(imagetext);
}

Result:
00:01:19.61 (1062397547)|USER_DEBUG|[82]|DEBUG|{
  "error": {
    "code": 400,
    "message": "Invalid JSON payload received. Expected a value.\n/9j/4AAQSkZJRgABAQAA\n^",
    "status": "INVALID_ARGUMENT"
  }
}
How can call google vision api from apex?
Thank you.
 
Best Answer chosen by dai tran 6
cvuyyurucvuyyuru
You need to prepare a JSON Request with Content and send that request to Google Vision API.
Check this documentation.
https://cloud.google.com/vision/docs/detecting-properties#vision-image-property-detection-protocol
http://prntscr.com/kg3iuo