• Pallavi Bharati 4
  • NEWBIE
  • 0 Points
  • Member since 2016

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 2
    Questions
  • 7
    Replies
I want to send jpg image to external system .
I have to convert that image to Base64 format .
Problem:-
a)Is it necessary to use document or Attachment for converting the image to base64.

Here's the Code:-
      if(Image2 != NULL)
                       {
           
                        Blob b=Blob.valueof(Image2);
                        req1.setBodyAsBlob(b);
                        fileString=EncodingUtil.Base64Encode(b);
                        System.debug('is now encoded as:'+fileString);
                       }
        
           String domainName1='52.6.251.159/~democrm/demo/vtigerdefault/services/rest/rest.php?';
           String endPointURL1='http://'+domainName1+'api_key=asws29qw3r545ndm&method=uploadImage&&image_name='+Image1+'&salesforce_id='+name3+'&image='+fileString;
        
           System.debug('>>>>>>>>>>>>>>>>>'+endPointURL1);
                          
            req1.setEndpoint(endPointURL1);
            req1.setMethod('POST'); 
            req1.setCompressed(true);

For converting image to base64 ,is this code is wrong..?

User-added image
Note:-Image2 is String data type.
         From choose file,selecting image

Give some ideas.!!
 
I am trying to push some records and image to external system using HTTP call.
1) I am having two webservice methods "insertOrUpdateData" and "uploadImage".One after another this two webservice method should execute
2)"insertOrUpdateData" this webservice callout is working,records inserting succesfully when i m testing in postman by giving salesforce_id
  records are coming in postman.
3)But i want  image (image will be convert into base64 format) and image name also inserts by clicking on save button from vf page at the same time.
4)i am creating new request for image and image name.
                so that after executing 1st request for "insertOrUpdateData"  2nd request  "uploadImage" should execute by clicking on save button

Problem:- I am not able to give responses properly

Below is the code :-

public class WebServiceCallout1 {
 
@future (callout=true)

 public static void sendNotification(String Unique,String Fax,String Phone,String Rating,string Email,String AnnualRevenue,String Description,String Image,String Imagename) {

 HttpRequest req = new HttpRequest();
        HttpResponse res = new HttpResponse();
        Http http = new Http();

 Lead sampleLead=[select id,LastName,Phone,Company,Email,S_Id__c,Status,Image__c,Image_name__c from lead where id=:Unique limit 1];

        system.debug('sample lead is----------'+sampleLead);       
        String name= Email;
        String name1=sampleLead.LastName;
        String name2=Phone; 
        String name3=sampleLead.S_Id__c;
        String name4=sampleLead.Company;
        String name5=Description;
        String name6=Fax;
        String name7=sampleLead.Status;
        String name8=Rating;
        String name9=AnnualRevenue;
        String Image1=sampleLead.Image_name__c;
        String Image2=sampleLead.Image__c;

String domainName='52.6.251.159/~democrm/demo/vtigerdefault/services/rest/rest.php?';
String endPointURL='http://'+domainName+'api_key=asws29qw3r545ndm&method=insertOrUpdateData&&email='+name+'&
last_name='+name1+'&phone='+name2+'&salesforce_id='+name3+'&company='+name4 +'&description='+name5+'&fax='+name6+'&status='+name7+'&rating='+name8+'&annual_revenue='+name9;

 System.debug('>>>>>>>>>>>>>>>>>'+endPointURL);
    req.setEndpoint(endPointURL);
    req.setMethod('POST');

//Another request
        HttpRequest req1 = new HttpRequest();
        HttpResponse res1 = new HttpResponse();
        Http http1 = new Http();
 
String domainName1='52.6.251.159/~democrm/demo/vtigerdefault/services/rest/rest.php?';
 String endPointURL1='http://'+domainName1+'api_key=asws29qw3r545ndm&method=uploadImage&&image_name='+Image1
+'&image='+Image2;
        
         System.debug('>>>>>>>>>>>>>>>>>'+endPointURL1);
           
               if(Image2 != NULL)
                       {
           //Converting image into base64
                        Blob b=Blob.valueof(Image2);
                        req.setBodyAsBlob(b);
                        String fileString=EncodingUtil.Base64Encode(b);
                        System.debug('is now encoded as:'+fileString);
                       }

                                  try {
                                           res = http.send(req);
                                                if(res.getStatusCode()== 200)
                                                           {
                                                             System.debug('SUCCESS!!!');
                                                                req1.setMethod('POST');               
                                                                req1.setEndpoint(endPointURL1);
                                                                req1.setCompressed(true);
                                                                res = http.send(req1);
                     System.debug('RESPONSE '+res.toString());
                                                               }
            
          } catch(System.CalloutException e) {
            System.debug('Callout error: '+ e);
            System.debug('RESPONSE '+res.toString());
        }
        
    }
 }

VF Page:-
User-added image
 
I want to send jpg image to external system .
I have to convert that image to Base64 format .
Problem:-
a)Is it necessary to use document or Attachment for converting the image to base64.

Here's the Code:-
      if(Image2 != NULL)
                       {
           
                        Blob b=Blob.valueof(Image2);
                        req1.setBodyAsBlob(b);
                        fileString=EncodingUtil.Base64Encode(b);
                        System.debug('is now encoded as:'+fileString);
                       }
        
           String domainName1='52.6.251.159/~democrm/demo/vtigerdefault/services/rest/rest.php?';
           String endPointURL1='http://'+domainName1+'api_key=asws29qw3r545ndm&method=uploadImage&&image_name='+Image1+'&salesforce_id='+name3+'&image='+fileString;
        
           System.debug('>>>>>>>>>>>>>>>>>'+endPointURL1);
                          
            req1.setEndpoint(endPointURL1);
            req1.setMethod('POST'); 
            req1.setCompressed(true);

For converting image to base64 ,is this code is wrong..?

User-added image
Note:-Image2 is String data type.
         From choose file,selecting image

Give some ideas.!!
 
I am trying to push some records and image to external system using HTTP call.
1) I am having two webservice methods "insertOrUpdateData" and "uploadImage".One after another this two webservice method should execute
2)"insertOrUpdateData" this webservice callout is working,records inserting succesfully when i m testing in postman by giving salesforce_id
  records are coming in postman.
3)But i want  image (image will be convert into base64 format) and image name also inserts by clicking on save button from vf page at the same time.
4)i am creating new request for image and image name.
                so that after executing 1st request for "insertOrUpdateData"  2nd request  "uploadImage" should execute by clicking on save button

Problem:- I am not able to give responses properly

Below is the code :-

public class WebServiceCallout1 {
 
@future (callout=true)

 public static void sendNotification(String Unique,String Fax,String Phone,String Rating,string Email,String AnnualRevenue,String Description,String Image,String Imagename) {

 HttpRequest req = new HttpRequest();
        HttpResponse res = new HttpResponse();
        Http http = new Http();

 Lead sampleLead=[select id,LastName,Phone,Company,Email,S_Id__c,Status,Image__c,Image_name__c from lead where id=:Unique limit 1];

        system.debug('sample lead is----------'+sampleLead);       
        String name= Email;
        String name1=sampleLead.LastName;
        String name2=Phone; 
        String name3=sampleLead.S_Id__c;
        String name4=sampleLead.Company;
        String name5=Description;
        String name6=Fax;
        String name7=sampleLead.Status;
        String name8=Rating;
        String name9=AnnualRevenue;
        String Image1=sampleLead.Image_name__c;
        String Image2=sampleLead.Image__c;

String domainName='52.6.251.159/~democrm/demo/vtigerdefault/services/rest/rest.php?';
String endPointURL='http://'+domainName+'api_key=asws29qw3r545ndm&method=insertOrUpdateData&&email='+name+'&
last_name='+name1+'&phone='+name2+'&salesforce_id='+name3+'&company='+name4 +'&description='+name5+'&fax='+name6+'&status='+name7+'&rating='+name8+'&annual_revenue='+name9;

 System.debug('>>>>>>>>>>>>>>>>>'+endPointURL);
    req.setEndpoint(endPointURL);
    req.setMethod('POST');

//Another request
        HttpRequest req1 = new HttpRequest();
        HttpResponse res1 = new HttpResponse();
        Http http1 = new Http();
 
String domainName1='52.6.251.159/~democrm/demo/vtigerdefault/services/rest/rest.php?';
 String endPointURL1='http://'+domainName1+'api_key=asws29qw3r545ndm&method=uploadImage&&image_name='+Image1
+'&image='+Image2;
        
         System.debug('>>>>>>>>>>>>>>>>>'+endPointURL1);
           
               if(Image2 != NULL)
                       {
           //Converting image into base64
                        Blob b=Blob.valueof(Image2);
                        req.setBodyAsBlob(b);
                        String fileString=EncodingUtil.Base64Encode(b);
                        System.debug('is now encoded as:'+fileString);
                       }

                                  try {
                                           res = http.send(req);
                                                if(res.getStatusCode()== 200)
                                                           {
                                                             System.debug('SUCCESS!!!');
                                                                req1.setMethod('POST');               
                                                                req1.setEndpoint(endPointURL1);
                                                                req1.setCompressed(true);
                                                                res = http.send(req1);
                     System.debug('RESPONSE '+res.toString());
                                                               }
            
          } catch(System.CalloutException e) {
            System.debug('Callout error: '+ e);
            System.debug('RESPONSE '+res.toString());
        }
        
    }
 }

VF Page:-
User-added image
 
I am not able to convert the rtaimage URL into the base64 encoding URL. Please have a look into below code and provide me the help on below issue. 

HttpRequest req = new HttpRequest();
String url = system.URL.getSalesforceBaseUrl().toExternalForm() + '/secur/frontdoor.jsp?sessionId=' + UserInfo.getSessionId() + '&retURL="/servlet/rtaImage?eid=5007000000uEmHK&feoid=00N70000002UTY9&refid=0EMc0000000DDdk"';
req.setEndpoint(url);
req.setHeader('Authorization', 'OAuth '+UserInfo.getSessionId());
req.setMethod('GET');
Http binding = new Http();
HttpResponse res = binding.send(req);
Blob image = res.getBodyAsBlob();
String base64Encode = EncodingUtil.base64Encode(image); 


String caseDescription = '<img alt="" src="data:' + res.getHeader('Content-Type') + ';' + base64Encode + '"></img>';
CaseDescRichTextStage__c stgObj = new CaseDescRichTextStage__c(CaseNumber__c = 'C-121', CaseDescBase64__c = caseDescription);

I am getting the below error while save this record with base64Encode URL.

Invalid data specified, the provided data does not seem to be a valid image: [B@254f39ef

I have also checked the value of res.getHeader('Content-Type'). It's giving me "text/html;charset=UTF-8" instead of 'image/png'