Search code examples
javascriptnode.jsexpressrequestjpeg

Handling an incoming JPEG image with Node JS


I am trying to get an imagen with a Post request using the library "request"

I get the image with a content-type: image/jpeg, which look as follows:

�PNG\r\n\n\rIHDR��\bL<�� IDATx��yxT���?���g2��!$$a�EP���-*j�ZkU�Vk-���\\j]Q���*�Zw�u�KqA@d��!�@BH�>�̝{���$�@��}&��s�3��=�y��+\\z�J� ��E��0\"'�����nr6�&�ʤmp!v��W����p��q���'� ͕�/�{�HM�om�z�?v���3>0º�V��z��U��:?���ǟ9���}C0�P�m�k��ּ7V?\b�ɧ�Hq�9ḣ�t?[?\t���5U$�Ԅ9�w�T�gg�C�zs3�G���\u000b۸쌡����F�7���QN�;{���V&��-C�54�d��\"2��ʨ35V�*��<���m|����M��vFo�Ywxa���F1�4��6�D�6������s��;�*xm��,��clnL�[����|�w7���g��هH��?��n��#x�y��_s�g�x������~�ɱ��v;���,��=ښ��������~y#����Y����������_|Γ�}�5g������_�����%x����3�b5--m?k:��~˪�\n��^��A��s�����{ú�?^�'�^{m����_��~�=�\\��nn��V�N'�P��\u000b2g��\r��E�8����Ł@*�|elZ��1�^��֪�H��GD����7�0bpb�՚�f�B\b5 jP�cQ�i�r��?���`�d�*��>�'�k۝�X���$\\XBĞ�<�F��#�/�SV�JQ��kg�Thh\r����;o,������F��i1�\r5�.m���/_\tc� �e\"�iĶE/k���b���\b<�^�����RR��ﯬ�ᤕ\t�Bed+�p{Gcud�}(����C�(�Kw��U�����pQ����W����EL,va�H�\n��V@П��\bJO�t9&�}U�<䁺�a*W&'ǃJ��<��j�F�A��EQH�N�ߗ�V�������@�b�]m�m���)���L��L�#�L�5Dehd+�w~QµLA?a�-�w\r\"sK�^���D뭚����Jp;# �\f*������fJ��1�x���@1f\\\\i�܂!�c�*DĄv��x2��*���q����+/�[0��W�I(�n����^���}�'�����l�,.��>�HI2��#s�ޑ;Qn�(�Eq�E2�a�;��Iٻ����m��\t�Ӊ(���vl6�P�H$¢E�8�3bd�p��\\jv��C� �W�[H��1\u000bc�RD�倉�&�Pl�Plq}��р�ވ�J|AD%44�vB՝�ۈ��i���aٸ*v�@Hr8�����K�c�w�c�`2��#j�ܭOl�����&y1�Q$��8�����RO(���\r��É���X����n5O�F+! fbV���� �ݝ=sP�X�W������,&�m�=��/x�^*j�:g���L��V\u000b�ur����Й��S`�4��X��K-\buy��F��fk\u000b/Rͨ�RlF��a\\�S�،�y��3ij�Փ�@�]���p���I�(���q�V��&����_N>��K~�f'��&...

Then, when I want to render it on an email template which is build with a template string, I don't find out how to put that image on it. I had found similar questions but this is always do in the client side.


Solution

  • There are three approaches you could take.

    In my order of preference (which happens to coincide with the best likely bandwidth costs):

    1. Save it somewhere where it will have a public HTTP(S) URL, then use that URL in the email.
    2. Make it one of the parts of your multi-part MIME email (presumably, if you are dealing with HTML formatted emails you are already using multi-part MIME so you can have a plain text alternative since not doing so is a red flag for spam filters) and then use a cid: scheme URL to reference it.
    3. Convert it to a base64 data: scheme URL and use that in the email.