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
sravanthi k 4sravanthi k 4 

Integration with rest api

Hi every one,

Trough REST API i am capturing url for a particular image now ,i want to read the data from that url in blob format and then attach that image in sales force ,

can any please help to read the data from url
This is my trigger:

trigger AttachmentImage on Item__c (before insert,before update) {
   list<Item__c>  lst=[select id,URL__c from Item__c];
 
    Blob pic ;
  pic =blob.valueof('http://multisite.dhrusoff.com/wp-content/plugins/forces/img/ballon1949004662-300X300.jpg');
       for(Item__c b:lst)
       {
   
       if(b.URL__c !=null) {
   
    //pic=EncodingUtil.base64Decode(b.URL__c);
         Attachment a = new Attachment (ParentId = b.id,Body = pic,Name = 'Item Image.jpeg');
          System.debug('==========Attach======= - ' +a);
          System.debug('==========Attach======= - ' +a.id);
        insert a;
     
         }
       
       }
Jai ChaturvediJai Chaturvedi
Hi,
 
public class ImageUri {
    public static String getDataUriFromUri(String url) {
        HttpRequest req = new HttpRequest();
        req.setEndpoint(url);
        req.setMethod('GET');
        Http binding = new Http();
        HttpResponse res = binding.send(req);
        Blob image = res.getBodyAsBlob();
        return 'data:'+res.getHeader('Content-Type')+';base64,'+EncodingUtil.base64Encode(image);
    }
}


Attachment n = new Attachment();
//You will want to tie your attachment to some type of custom or standard object
//n.ParentId = myAccount.Id;
n.Name ='myImage.jpg';
n.Body = image;
//If we were saving a PDF as an attachment the ContentType would be'pdf'
n.contentType ='image/jpeg';
insert n;


Thanks
Jai