Target response content to base64string

Not applicable

Hi There,

our target endpoint is returning contents of pdf file as binary stream in httpResponse. I want to convert response from proxy as json payload from where I want to return contents as base64string.

I tried various base64 libraries but looks like I am doing something fundamentally here. I am using javascript policy with following code

var documentContent = response.content;

var encodedContent = Base64.encode(documentContent)

context.setVariable('raw.response', encodedContent);

var documentType = context.getVariable("response.header.Content-type");

var newResponse = {

"code": 200,

"message": null,

"documentType": documentType,

"content": encodedContent

};

response.content = JSON.stringify(newResponse);

/** * * Base64 encode / decode * http://www.webtoolkit.info/ * **/

Has anyone convert this to base64string?

0 5 2,060
5 REPLIES 5

Not applicable

Hi @harmeet.sra12 - You can leverage Base64 encoding libraries for your solution. In order to include the library into your API Proxy please follow these directions:

1) Include Base64 JS Library. You can download this library. https://github.com/dankogai/js-base64

2) Include the library as part of the API Proxy Resources. You can also upload the library at the org and environment level.

3) Reference the library from the JS Policy:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Javascript async="false" 
        continueOnError="false" enabled="true" timeLimit="200" 
        name="JavaScript-1">
    <DisplayName>JavaScript 1</DisplayName>
    <Properties>
        <Property name="propName">propertyValue</Property>
    </Properties>
    <ResourceURL>jsc://my-javascript-source-file</ResourceURL>
    <IncludeURL>jsc://a-javascript-library-file</IncludeURL>
</Javascript>

4) Start Base64 encoding as your code above

Base64.encode('dankogai');  // ZGFua29nYWk=

Hope it helps!

Thanks @Maruti Chand for bringing it to my attention!

Thanks Diego Zuluaga,

I don't have string to here, I am getting binary stream. String encoding is working fine but when I convert streamed content to base64string using above library it looks like valid base64string but when I decode back to binary stream and save it to file, I can't really see anything in file.

following are original and decoded part of content of file

original file content

endobj 5 0 obj << /N 3 /Length 11 0 R /Filter /FlateDecode >> stream xœ–wXSçÇßsNö`$!l{†¥@ ‘¦€ Ù¢’  $ ÷@T°¢¨ÈR)ŠX°Z†Ô‰(ŠâÞ R”Z¬âÂÑDž§õööÞÛÛïç|žßûû½çý÷y ¤€L®0V@(’ˆ#ü½±qñ ì €`€= nn¶WXX0+ЗÍÈ•;Ñ«› R¼¯1{ÿOªÜl± (Lγxü\®œ‹äœ™/ÉVØ'åLKÎP0ŒR°X~@9k(8u†­?ûÌ°§‚yBOΑrÎæy î•ó†<)_Έ"—â<?_Î×ålœ) äüF+äsä9 H »„ÏM“³œIâȶœç €#¥~ÁÉ_°„_ Q$ÅÎÊ. RÓ$ s®ÃÞÅ…Åàçgò%f‡›Áóì,a6GTÀLΟEQÔ–!/²“½‹“ÓÁÆþ‹Bý×Å¿)EogèEøçžAôþ?lå—Õ kJ^›-Ø’« è\€ÆÝ?lÆ{ P–÷­ãòùÐó’&‘d»ÚÚæççÛø\EA×ÿtøúâ{6Ší~/ǟ‘fJŠºq³2³¤bFn6‡Ëg0ÿ<Äÿ8ð¯ÏaÁOá‹ù"yD´|Ê¢Ty»E<D%bDÿ©‰ÿ0ìOš™k¹¨-Ѩ\¦äç~€¢’°[¾ýÞ`|4PܼýÑ™¹ÿ,èßw…ˏ\Aêç8vD$ƒ+çͬ)®%@P4 ô€0Là œð¾`‘ ,\„@ òÁ2°ƒR°ì Õ 4‚fÐ ƒNp œçÀ%pÜ ÷€ Œ€§`¼Óa!2D…4!}Ȳ‚ 4ò…‚¡(J‚R!$…–Ak¡R¨ª†ê¡fè[è(tº Bw !húz#0¦Áº°)l ³`/8Ž„Á©p¼.‚7Õp|î€O×à° ~ O! !"tÄ a",„„"ñH "FV %HÒ€´"ÝHr

Decoded content

endobj 5 0 obj << /N 3 /Length 11 0 R /Filter /FlateDecode >> stream xýýýwXSýýýsNý`$!l{ýý@ ýýý bý  $ ý@TýýýýR)ýXýZý(ýýý RýZýýýýDýýýýýýýýýý|ýýýýýýýy ýýLý0V@(ýý#ýýqý ý ý`ý= nnýWXX0ý+ý;ýký Rýý1{ýýOýýlý (L³xý\ýýý/ýVý'ýLKýP0ýRýX~@9k(8uýý?ý0ýýyBO‘rýýy ýý<)_ˆ"ýý<?_ýýýlý) ýýF+ýsý9ýH ýýýMýýýýIýýýýý ý#ý~ýý_ýý_ Q$ýýý. Rý$ sýýýEýýýgý%fýýýýý,a6GTýLŸEQ!/ýýýýýýýýýýBýý)EogýEýýAýý?lýý kJ^ý-ý ý\ýýý?lý{ Pýýýýýýýý&ýdýýýýýýýý\EAýýtýýý{6ýý~/Çý‘fJýýqý2ýýbFn6ýýg0ý<ýý8ýýaýOýý"yDý|ýýTyýE<ýDý%bDýýýý0ýOýýkýýý-ýý\ýýý~ýýýý[ýý`|4P<ýYýý,ýýwýýý\Aýý8vD$ý+ýl)ý%@P4ýý0Lý ýýýý`ý ,\ýý@ ýý2ýýRýý `4ýfý ýNp ýýý%pý ýý ýýý`ýýa!2Dý4!}ýýý 4Ô¡(JýR!$ýýAkýRýýýýfý[ý(tý Bwý!hýz#0ýýýý)l ý`/8ýýýýpý.ý7Õp|ýO×ýý ~ O! !"tý a",ýýý"ýH "FV %H€ý"ýHr

How did you resolve pdf content issue?

I'm getting error "Cannot modify a property of a sealed object: Base64." in line no 157

Not applicable

@vinay poreddy I had to use custom Java callout policy where I could convert stream to base64string.

 public ExecutionResult execute(MessageContext messageContext, ExecutionContext executionContext) {
        InputStream inputStream = messageContext.getResponseMessage().getContentAsStream();
        BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream);
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        int b = 0;
        try {
            while( (b = bufferedInputStream.read()) != -1){
                byteArrayOutputStream.write(b);
            }
        } catch (IOException e) {
            return ExecutionResult.ABORT;
        }
        byte[] base64Encoded = Base64.encodeBase64(byteArrayOutputStream.toByteArray());
        messageContext.getResponseMessage().setContent(new ByteArrayInputStream(base64Encoded));
        return ExecutionResult.SUCCESS;
    }

Hope this helps.


Thanks

Harmeet