Skip to content

Instantly share code, notes, and snippets.

@lukethacoder
Created August 1, 2022 05:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lukethacoder/ba884665f4565888a6e8eefff90166c1 to your computer and use it in GitHub Desktop.
Save lukethacoder/ba884665f4565888a6e8eefff90166c1 to your computer and use it in GitHub Desktop.
APEX: Image content from URL
String extFileUrl = 'https://images.unsplash.com/photo-1592746455916-7ac99236b6d6?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=634&q=80';
Http h = new Http();
HttpRequest req = new HttpRequest();
// Replace any spaces in extFileUrl with %20
extFileUrl = extFileUrl.replace(' ', '%20');
// Set the end point URL
req.setEndpoint(extFileUrl);
req.setMethod('GET');
req.setHeader('Content-Type', 'application/' + fileContentType);
req.setCompressed(true);
req.setTimeout(60000);
// Now Send HTTP Request
HttpResponse res = h.send(req);
System.debug('Response from Server: ' + res.getBody());
// getBodyAsBlob method was will convert the response into Blob
Blob fileBlob = res.getBodyAsBlob();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment