+ Start a Discussion

Convert string to Blob properly

Hi All,


I am getting binary (.doc) file from external API by my APEX code.


Then I convert it to Blob format and save as an attachment.

When I try to download and open it with Word editor it tells this file is corrupted.


My code to convert string from external API to Blob:


Blob body = Blob.valueOf(httpResponse.getBody());

insert new Attachment(
        Name = filename,
        Body = body,
        ParentId = parentId);

 How can I save it in a right format? I saw there are a lot of special characters in response from external API like:


PK!Ҩ0��	[Content_Types].xml �(��V�n�0�W�?D�V�@���8�ql�J?����ؖ����Q�BB��HQ��ٙݍ��*ς8��^�ehiT��1��t�Y�Qh%2�!fk�l8����k>����l�h8�r�𡱠�Kb\.�^ݔ[!?�x�۽��h�,0� z#.U���W�_�xbj��C�c�cy�����6K�@�Z� ��$I%(#�9Q��uF��TZ���M��$b�a��"m�2�7�m�!��(����������SUWs�;r.R��P���p����TA7�����h����j�Sw�u(��3�s���C�b�







Ypu want may to use content type attribute to get it in proper format





aha - the old Black diamond with a question mark.  


This character is the UTF-8 fallback character U+FFFD and can occur when a string is passed to SFDC that is not in UTF-8.  For example, if the input data is encoded in ISO 8859-1 or Windows-1252, any character in those sets with hex value > 7F (basically, any non A-Z, a-z, 0-9, and common punctuation) will be interpreted by SFDC UTF-8 encoding as an invalid character and be replaced by U+FFFD.