Skip to Content
0
Dec 02, 2021 at 07:52 AM

Data returned from Gateway get stream contains replacement characters

445 Views

Hi experts,

This is related to my previous question.

I have a requirement to get files (word, excel, pdf, png, etc.) from Gateway (S/4HANA system) via CAP.

While experimenting, I noticed that media data returned from the Gateway contains replacement characters (U+FFFD), indicating that some characters were not recognized properly.

�PNG


IHDR,,y}�usRGB���bKGD�������	pHYs��tIME�
d��^tEXtCommentCreated with GIMPW��IDATx������}������r�j�֫��
Ik�ml�1��]3���D�����[u��u.�Z���iFŶ��-0h]g�舕

Correct data is:

PNG


IHDR,,y}usRGBbKGD	pHYstIME
d^tEXtCommentCreated with GIMPWIDATx}rj֫
Ikml1]3D[uu.ZiFŶ-0h]g舕

This happens not only with custom OData but also standard OData service such as CV_ATTACHMENT_SRV.

The following is sample CAP code used to display the raw data returned from the Gateway.

    this.on('READ', 'TempFile', async (req, next) => {
        const columns = req.query.SELECT.columns
        if (columns && columns[0].ref[0] === 'content') {

            const response = await core.executeHttpRequest({ destinationName: 'CC_S4_2020_FPS01'},{
                method: 'GET',
                url: `/sap/opu/odata/sap/CV_ATTACHMENT_SRV/OriginalContentSet(Documenttype='GOS',Documentnumber='EXT46000000000152',Documentpart='',Documentversion='',ApplicationId='0AB16793A5871EEC94E76410B44D5762',FileId='0AB16793A5871EEC94E76410B44D7762')/$value`
            },{
                fetchCsrfToken: true
            })            
            return _createStream(response)        
        }        
         else {
                return next()
        }
    })

    function _createStream (response) {       
        const original = new Buffer.from(response.data) 
        const base64data = original.toString('base64')
        const base64buffer = new Buffer.from(base64data, 'base64')
        console.log('original data: ', response.data)
        console.log('base64 encoded data: ', base64data)
        const output = response.data
        
        const stream = new Readable()
        const result = new Array()
        stream.push(output)
        stream.push(null)       
        result.push({ value: stream })

        return result
    }

If someone knows the reason behind this and hopefully how to fix this, please let me know.

Best regards,

Mio