Created
August 1, 2022 05:29
-
-
Save lukethacoder/ba884665f4565888a6e8eefff90166c1 to your computer and use it in GitHub Desktop.
APEX: Image content from URL
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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