1

How can I read the content of multipart/related response? Response starts with:

--9d567d37-88c2-4dd6-91f0-9075025b081b
Content-Location: https://test.com.br
Content-ID: <a8298b3a-9577-4d79-8c43-e581a32b84f5@resteasy-multipart>
Content-Type: image/jpeg

Edit

The Response header ContentType is:

content-type: multipart/related;start="<8db0cf57-06f2-402e-be99-5d6cb387c2b7@resteasy-multipart>";type="image/jpeg"; boundary=12af3dc4-8f2e-446f-b078-bed5b580af35

Edit 2

Body response, the full body have 1MB, so I summarized here; The content is binary jpeg, and I don't how to put it here right encoded:

--12af3dc4-8f2e-446f-b078-bed5b580af35
Content-Location: https://....
Content-ID: <8db0cf57-06f2-402e-be99-5d6cb387c2b7@resteasy-multipart>
Content-Type: image/jpeg

ÿØÿàJFIFÿÛC     

 $.' ",#(7),01444'9=82<.342ÿÀ   ôôÿÄ    
ÿĵ}!1AQa"q2¡#B±ÁRÑð$3br    
%&'()*456789:CDEFGHIJSTUVWXYZcdefghijstuvwxyz¢£¤¥¦§¨©ª²³´µ¶·¸¹ºÂÃÄÅÆÇÈÉÊÒÓÔÕÖרÙÚáâãäåæçèéêñòóôõö÷øùúÿÚ?ã<]bVþSÓæÿ
à.b*Æ©0Á¤¢(Í(4ê/n´¡©wSÑ»4h'i>ôÜûѺô¤ÝM&9¢(¢ ¥òÍ8BÄÓÅ¿½9`§ù=p*D·ÝÏJ¤*«ÖÜ
fÜB£Ëçÿ+JifÊì8[1ëNû8£ÉÇjQG¥;Èt¥òE8FÖCOXy9<Æ õ£ÊÀâåäc¾W~áÐcQJcö GO   6äÓü¼þ4¢!i@téNòøëHaÎMq¶iÂ!´ñB#4íbQqèhnÉ4ª¸¡WOíÏ#X¦h+
zõ¦ùçvÁô ¨#iÐæ ô¢0G^qÖ¤ò
(RÞ<Ó¶ãØÔlÄð)J»&    ôæ`Ï­ OÎå#½
###ommited body here 1MB data###
~���"�r���i�O>�֝ ��FP3���޹�K���O_�MvA�h����:�A�jw�'>�).9��0:�i���j&���������FNS��3�;��~t��q�K�`r;�I�Ʃ����c+�5(����ڂ��N8��XL��C�A v��`��I��5(���LU
�t���p:P�#��`7JTn�T@�A�G�#ژ�"�9ɨ�?6���i��4ά}�?� 4�ى�=i���"�~?�Қ�zJ��+�J���֡?t��nv�V���H����+׽T�����T���=����j_���Tu�>�zF�x���Z��?�֠��G��Z�ҝ����k����������Q��ڙ���Zg��z�I���
0W��
--12af3dc4-8f2e-446f-b078-bed5b580af35--
Community
  • 1
  • 1
  • What request are you making that is giving you the response? Maybe this will help you: https://stackoverflow.com/questions/220231/accessing-the-web-pages-http-headers-in-javascript?lq=1 – kmoser Jun 03 '20 at 19:35
  • I am using the fetch api with response.blob() doesn't work. The URL is sensitive, and authenticated so I can not post full code here, I solved calling another URL but, I want to get same infos about that contentType. I found same soluctions in Python, but not in JS – anderson_cvl Jun 04 '20 at 20:46
  • Can you just parse the response as a giant string and truncate all the headers (presumably separated from the content by a blank line) to leave only the actual content? – kmoser Jun 05 '20 at 04:00

1 Answers1

0

If i understood correctly, you want to get an image from server and use it like a preview for example. For this purpose you should set Response-Type header as 'blob':

Response-Type: 'blob'

And then to get base64 string you can use URL.createObjectUrl(Blob) or FileReader. More info and differences here

That's how you can do that via axios and FileReader:

const response = await axois.get('/your/url', {
  getParams,
  baseURL: 'https://www.your-base-url.com',
  responseType: 'blob'
});

const fileReader = new FileReader();
fileReader.readAsDataURL(response.data);
fileReader.onloadend = () => {
  const imageAsBase64String = reader.result;
};
  • the result is data:multipart/related;base64, instead of image/jpeg, I suspect that file reader is including the all content of response and not only the jpeg content – anderson_cvl Jun 03 '20 at 21:06
  • Could you show a request (with headers and body) and response info from Network section in Devtools in Chrome? – Ivan Vekshin Jun 06 '20 at 12:23