System.StringException: Unrecognized base64 character:












1















I am trying to parse the response body which is in "base64 content of a file" and trying to create the attachment with the body but getting error of " Unrecognized base64 character: " I have included sample code of the response



"JVBERi0xLjQKJeLjz9MKNCAwIG9iaiA8PC9GaWx0ZXIvRmxhdGVEZWNvZGUvTGVuZ3RoIDUzOTg+PnN0cmVhbQp4nM1dzXLbuLLe+ymwueWkyuGIFCVRs9M4mjk6Zcsex557qyZnQZOUzBRFyiCp69zHyJPMI3kZzuI8wwVIQKQ+AKIyk8WppKpBoPvrrxvNHxEk/Xz20/3ZgEymDrkPz+b3Z7+ePZ8NrMFwMiL/e+aQf7LBT2f2gFyf/f6vAQnPhmMyGY3J5mw0aVpJ3RqOrYHL2sODZjP+dPbfZynD4f/omjv84Web2J7FfNyvzux6wCbexHJHZDJ0uLjfnL25vFne383uyfs5YfK32eKKzMj7h7v5nHXdz++uF8v5/O39J07bBOo4U2s8JRN7YE28GvV+fn37gdzO7u4X8yth7TpkzNw7PC7bHVueLbeTdpuJEduudbttofd0tpIsPMs5ICHUxpOawJxFNSdX8w/kw83Dhw+LX5as+eNhHDqEiWu5NcCVT/IsiKuiIvPrJVlGRZF99tdRF8LRQoxHlt2QuIpIHn9dRzWSn5AoL1hHTt6RfpTRyJo2M7TYbPyCxkGZVBHxS3JnXVofLIZZ5iSJy.....


When i am trying to use this response body https://www.freeformatter.com/base64-encoder.html then i am able to download the pdf but getting error when trying to decode it from Sales force



Here is the code which i have written



List<Object> filesList = (List<Object>)responseMap.get('files');
Map<String, Object> files = (Map<String, Object>)filesList.get(0);
//String fileId = (String)files.get('id');
string fileId='92080882-cc3c-407d-9552-63ee02064733';
Http http = new Http();
HttpRequest request = new HttpRequest();
request.setEndpoint(youSignSetting[0].Base_URL__c+'/files/'+fileId+'/download');
request.setMethod('GET');
request.setheader('Content-Type', 'application/json; charset=UTF-8');
request.setHeader('Accept', 'application/json');
request.setHeader('Authorization','Bearer '+key);
request.setTimeOut(120000);
System.debug(' Procedure Creation '+request.getBody());
System.debug(' Procedure Creation '+request.getEndpoint());
System.debug(' Procedure Creation '+request.getHeader('Authorization'));
HttpResponse response1 = http.send(request);
if (response1.getStatusCode() == 200) {
String responseBody = response1.getbody();
//Encodingbody=EncodingUtil.base64Decode(responseBody);
string responsestring=responsebody.remove('"');
system.debug('Response body---->'+responseBody);
Attachment att = new Attachment();
att.ParentId = yp.ID_parent_attachment__c;
att.Name = yp.Name+' Attachment.pdf';
blob body=EncodingUtil.base64Decode(responsestring);

att.Body = body;

insert att;
}
else{
System.debug('The status code returned was not expected: ' +
response1.getStatusCode() + ' ' + response1.getStatus());
System.debug(response1.getBody());
}
}
}else{
System.debug('#### Start Procedure Error Message => '+response.ErrorMessage);
}
}
}


I have reviewed many articles regarding this but unable to get any result










share|improve this question



























    1















    I am trying to parse the response body which is in "base64 content of a file" and trying to create the attachment with the body but getting error of " Unrecognized base64 character: " I have included sample code of the response



    "JVBERi0xLjQKJeLjz9MKNCAwIG9iaiA8PC9GaWx0ZXIvRmxhdGVEZWNvZGUvTGVuZ3RoIDUzOTg+PnN0cmVhbQp4nM1dzXLbuLLe+ymwueWkyuGIFCVRs9M4mjk6Zcsex557qyZnQZOUzBRFyiCp69zHyJPMI3kZzuI8wwVIQKQ+AKIyk8WppKpBoPvrrxvNHxEk/Xz20/3ZgEymDrkPz+b3Z7+ePZ8NrMFwMiL/e+aQf7LBT2f2gFyf/f6vAQnPhmMyGY3J5mw0aVpJ3RqOrYHL2sODZjP+dPbfZynD4f/omjv84Web2J7FfNyvzux6wCbexHJHZDJ0uLjfnL25vFne383uyfs5YfK32eKKzMj7h7v5nHXdz++uF8v5/O39J07bBOo4U2s8JRN7YE28GvV+fn37gdzO7u4X8yth7TpkzNw7PC7bHVueLbeTdpuJEduudbttofd0tpIsPMs5ICHUxpOawJxFNSdX8w/kw83Dhw+LX5as+eNhHDqEiWu5NcCVT/IsiKuiIvPrJVlGRZF99tdRF8LRQoxHlt2QuIpIHn9dRzWSn5AoL1hHTt6RfpTRyJo2M7TYbPyCxkGZVBHxS3JnXVofLIZZ5iSJy.....


    When i am trying to use this response body https://www.freeformatter.com/base64-encoder.html then i am able to download the pdf but getting error when trying to decode it from Sales force



    Here is the code which i have written



    List<Object> filesList = (List<Object>)responseMap.get('files');
    Map<String, Object> files = (Map<String, Object>)filesList.get(0);
    //String fileId = (String)files.get('id');
    string fileId='92080882-cc3c-407d-9552-63ee02064733';
    Http http = new Http();
    HttpRequest request = new HttpRequest();
    request.setEndpoint(youSignSetting[0].Base_URL__c+'/files/'+fileId+'/download');
    request.setMethod('GET');
    request.setheader('Content-Type', 'application/json; charset=UTF-8');
    request.setHeader('Accept', 'application/json');
    request.setHeader('Authorization','Bearer '+key);
    request.setTimeOut(120000);
    System.debug(' Procedure Creation '+request.getBody());
    System.debug(' Procedure Creation '+request.getEndpoint());
    System.debug(' Procedure Creation '+request.getHeader('Authorization'));
    HttpResponse response1 = http.send(request);
    if (response1.getStatusCode() == 200) {
    String responseBody = response1.getbody();
    //Encodingbody=EncodingUtil.base64Decode(responseBody);
    string responsestring=responsebody.remove('"');
    system.debug('Response body---->'+responseBody);
    Attachment att = new Attachment();
    att.ParentId = yp.ID_parent_attachment__c;
    att.Name = yp.Name+' Attachment.pdf';
    blob body=EncodingUtil.base64Decode(responsestring);

    att.Body = body;

    insert att;
    }
    else{
    System.debug('The status code returned was not expected: ' +
    response1.getStatusCode() + ' ' + response1.getStatus());
    System.debug(response1.getBody());
    }
    }
    }else{
    System.debug('#### Start Procedure Error Message => '+response.ErrorMessage);
    }
    }
    }


    I have reviewed many articles regarding this but unable to get any result










    share|improve this question

























      1












      1








      1








      I am trying to parse the response body which is in "base64 content of a file" and trying to create the attachment with the body but getting error of " Unrecognized base64 character: " I have included sample code of the response



      "JVBERi0xLjQKJeLjz9MKNCAwIG9iaiA8PC9GaWx0ZXIvRmxhdGVEZWNvZGUvTGVuZ3RoIDUzOTg+PnN0cmVhbQp4nM1dzXLbuLLe+ymwueWkyuGIFCVRs9M4mjk6Zcsex557qyZnQZOUzBRFyiCp69zHyJPMI3kZzuI8wwVIQKQ+AKIyk8WppKpBoPvrrxvNHxEk/Xz20/3ZgEymDrkPz+b3Z7+ePZ8NrMFwMiL/e+aQf7LBT2f2gFyf/f6vAQnPhmMyGY3J5mw0aVpJ3RqOrYHL2sODZjP+dPbfZynD4f/omjv84Web2J7FfNyvzux6wCbexHJHZDJ0uLjfnL25vFne383uyfs5YfK32eKKzMj7h7v5nHXdz++uF8v5/O39J07bBOo4U2s8JRN7YE28GvV+fn37gdzO7u4X8yth7TpkzNw7PC7bHVueLbeTdpuJEduudbttofd0tpIsPMs5ICHUxpOawJxFNSdX8w/kw83Dhw+LX5as+eNhHDqEiWu5NcCVT/IsiKuiIvPrJVlGRZF99tdRF8LRQoxHlt2QuIpIHn9dRzWSn5AoL1hHTt6RfpTRyJo2M7TYbPyCxkGZVBHxS3JnXVofLIZZ5iSJy.....


      When i am trying to use this response body https://www.freeformatter.com/base64-encoder.html then i am able to download the pdf but getting error when trying to decode it from Sales force



      Here is the code which i have written



      List<Object> filesList = (List<Object>)responseMap.get('files');
      Map<String, Object> files = (Map<String, Object>)filesList.get(0);
      //String fileId = (String)files.get('id');
      string fileId='92080882-cc3c-407d-9552-63ee02064733';
      Http http = new Http();
      HttpRequest request = new HttpRequest();
      request.setEndpoint(youSignSetting[0].Base_URL__c+'/files/'+fileId+'/download');
      request.setMethod('GET');
      request.setheader('Content-Type', 'application/json; charset=UTF-8');
      request.setHeader('Accept', 'application/json');
      request.setHeader('Authorization','Bearer '+key);
      request.setTimeOut(120000);
      System.debug(' Procedure Creation '+request.getBody());
      System.debug(' Procedure Creation '+request.getEndpoint());
      System.debug(' Procedure Creation '+request.getHeader('Authorization'));
      HttpResponse response1 = http.send(request);
      if (response1.getStatusCode() == 200) {
      String responseBody = response1.getbody();
      //Encodingbody=EncodingUtil.base64Decode(responseBody);
      string responsestring=responsebody.remove('"');
      system.debug('Response body---->'+responseBody);
      Attachment att = new Attachment();
      att.ParentId = yp.ID_parent_attachment__c;
      att.Name = yp.Name+' Attachment.pdf';
      blob body=EncodingUtil.base64Decode(responsestring);

      att.Body = body;

      insert att;
      }
      else{
      System.debug('The status code returned was not expected: ' +
      response1.getStatusCode() + ' ' + response1.getStatus());
      System.debug(response1.getBody());
      }
      }
      }else{
      System.debug('#### Start Procedure Error Message => '+response.ErrorMessage);
      }
      }
      }


      I have reviewed many articles regarding this but unable to get any result










      share|improve this question














      I am trying to parse the response body which is in "base64 content of a file" and trying to create the attachment with the body but getting error of " Unrecognized base64 character: " I have included sample code of the response



      "JVBERi0xLjQKJeLjz9MKNCAwIG9iaiA8PC9GaWx0ZXIvRmxhdGVEZWNvZGUvTGVuZ3RoIDUzOTg+PnN0cmVhbQp4nM1dzXLbuLLe+ymwueWkyuGIFCVRs9M4mjk6Zcsex557qyZnQZOUzBRFyiCp69zHyJPMI3kZzuI8wwVIQKQ+AKIyk8WppKpBoPvrrxvNHxEk/Xz20/3ZgEymDrkPz+b3Z7+ePZ8NrMFwMiL/e+aQf7LBT2f2gFyf/f6vAQnPhmMyGY3J5mw0aVpJ3RqOrYHL2sODZjP+dPbfZynD4f/omjv84Web2J7FfNyvzux6wCbexHJHZDJ0uLjfnL25vFne383uyfs5YfK32eKKzMj7h7v5nHXdz++uF8v5/O39J07bBOo4U2s8JRN7YE28GvV+fn37gdzO7u4X8yth7TpkzNw7PC7bHVueLbeTdpuJEduudbttofd0tpIsPMs5ICHUxpOawJxFNSdX8w/kw83Dhw+LX5as+eNhHDqEiWu5NcCVT/IsiKuiIvPrJVlGRZF99tdRF8LRQoxHlt2QuIpIHn9dRzWSn5AoL1hHTt6RfpTRyJo2M7TYbPyCxkGZVBHxS3JnXVofLIZZ5iSJy.....


      When i am trying to use this response body https://www.freeformatter.com/base64-encoder.html then i am able to download the pdf but getting error when trying to decode it from Sales force



      Here is the code which i have written



      List<Object> filesList = (List<Object>)responseMap.get('files');
      Map<String, Object> files = (Map<String, Object>)filesList.get(0);
      //String fileId = (String)files.get('id');
      string fileId='92080882-cc3c-407d-9552-63ee02064733';
      Http http = new Http();
      HttpRequest request = new HttpRequest();
      request.setEndpoint(youSignSetting[0].Base_URL__c+'/files/'+fileId+'/download');
      request.setMethod('GET');
      request.setheader('Content-Type', 'application/json; charset=UTF-8');
      request.setHeader('Accept', 'application/json');
      request.setHeader('Authorization','Bearer '+key);
      request.setTimeOut(120000);
      System.debug(' Procedure Creation '+request.getBody());
      System.debug(' Procedure Creation '+request.getEndpoint());
      System.debug(' Procedure Creation '+request.getHeader('Authorization'));
      HttpResponse response1 = http.send(request);
      if (response1.getStatusCode() == 200) {
      String responseBody = response1.getbody();
      //Encodingbody=EncodingUtil.base64Decode(responseBody);
      string responsestring=responsebody.remove('"');
      system.debug('Response body---->'+responseBody);
      Attachment att = new Attachment();
      att.ParentId = yp.ID_parent_attachment__c;
      att.Name = yp.Name+' Attachment.pdf';
      blob body=EncodingUtil.base64Decode(responsestring);

      att.Body = body;

      insert att;
      }
      else{
      System.debug('The status code returned was not expected: ' +
      response1.getStatusCode() + ' ' + response1.getStatus());
      System.debug(response1.getBody());
      }
      }
      }else{
      System.debug('#### Start Procedure Error Message => '+response.ErrorMessage);
      }
      }
      }


      I have reviewed many articles regarding this but unable to get any result







      apex httpresponse base64






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 27 '18 at 16:43









      AveshAvesh

      357




      357






















          1 Answer
          1






          active

          oldest

          votes


















          5














          The content you are receiving is not Base64: it is Base64 inside JSON, which you are asking the server to provide to you. Base64 encoding uses the characters [A-Za-z0-9+/]. The backslash and quote characters shown here are not part of the Base64 encoding but the JSON wrapper around it.



          You need to deserialize the JSON first, into a String, and then Base64-decode that String:



          String base64Content = JSON.deserialize(response1.getBody(), String.class));
          Blob body = EncodingUtil.base64Decode(base64Content);





          share|improve this answer























            Your Answer








            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "459"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: false,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsalesforce.stackexchange.com%2fquestions%2f240678%2fsystem-stringexception-unrecognized-base64-character%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            5














            The content you are receiving is not Base64: it is Base64 inside JSON, which you are asking the server to provide to you. Base64 encoding uses the characters [A-Za-z0-9+/]. The backslash and quote characters shown here are not part of the Base64 encoding but the JSON wrapper around it.



            You need to deserialize the JSON first, into a String, and then Base64-decode that String:



            String base64Content = JSON.deserialize(response1.getBody(), String.class));
            Blob body = EncodingUtil.base64Decode(base64Content);





            share|improve this answer




























              5














              The content you are receiving is not Base64: it is Base64 inside JSON, which you are asking the server to provide to you. Base64 encoding uses the characters [A-Za-z0-9+/]. The backslash and quote characters shown here are not part of the Base64 encoding but the JSON wrapper around it.



              You need to deserialize the JSON first, into a String, and then Base64-decode that String:



              String base64Content = JSON.deserialize(response1.getBody(), String.class));
              Blob body = EncodingUtil.base64Decode(base64Content);





              share|improve this answer


























                5












                5








                5







                The content you are receiving is not Base64: it is Base64 inside JSON, which you are asking the server to provide to you. Base64 encoding uses the characters [A-Za-z0-9+/]. The backslash and quote characters shown here are not part of the Base64 encoding but the JSON wrapper around it.



                You need to deserialize the JSON first, into a String, and then Base64-decode that String:



                String base64Content = JSON.deserialize(response1.getBody(), String.class));
                Blob body = EncodingUtil.base64Decode(base64Content);





                share|improve this answer













                The content you are receiving is not Base64: it is Base64 inside JSON, which you are asking the server to provide to you. Base64 encoding uses the characters [A-Za-z0-9+/]. The backslash and quote characters shown here are not part of the Base64 encoding but the JSON wrapper around it.



                You need to deserialize the JSON first, into a String, and then Base64-decode that String:



                String base64Content = JSON.deserialize(response1.getBody(), String.class));
                Blob body = EncodingUtil.base64Decode(base64Content);






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 27 '18 at 16:52









                David ReedDavid Reed

                36.4k72254




                36.4k72254






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Salesforce Stack Exchange!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsalesforce.stackexchange.com%2fquestions%2f240678%2fsystem-stringexception-unrecognized-base64-character%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks

                    Calculate evaluation metrics using cross_val_predict sklearn

                    Insert data from modal to MySQL (multiple modal on website)