Decode gzip from string in request json body

Hello All,

The API Proxy we have is receiving a JSON body in the request. One component in the JSON body is a string.

{

"event_id": 1234

"event": "H4sIAAAAAAAAA8pIzcnJVyjPL8pJAQAAAP//AwCFEUoNCwAAAA=="

}

We need to decode the "event" string to a text to be able to parse and process it. 

How can we do that in Apigee?

Solved Solved
1 4 736
1 ACCEPTED SOLUTION

Two things

  • atob is not available in the JS Callout.  The runtime is Rhino, not v8. You won't have all the JS builtins. Check the Apigee documentation for details. There are other ways to base64-decode data in Apigee.
  • But it seems like you have a data stream which is gzipped-then-base64encoded and you want to decode it.  Base64-decoding only gets you to a gzipped (compressed) data stream.  AFAIK there is no way using Apigee builtin capabilities to gunzip a data stream in Apigee.  You could write a Java callout to do it, easily. 

What are you really trying to do?  What is the "requestXML" thing?  Are you trying to decode a gzipped-then-base64-encoded SAML Assertion? If so, this pre-built Java callout may help you:  Apigee Java SAML Decoder. It does the base64-decode-then-gunzip thing. In the case of a data stream like PFJlc3BvbnNlIERlc3Rp.... It produces an XML assertion. 

View solution in original post

4 REPLIES 4

To decode the "event" field which is in base64 format in Apigee, you can follow these steps:

Extract Base64 Content: First, extract the base64 string value of the "event" field from the JSON request body.

Decode the Base64: Use a JavaScript policy in Apigee to decode the base64 content. You can do this using the atob() function in JavaScript.

Handle the Decoded Result: After decoding the base64 string, you will get the original text. Now, you can manipulate or process this text as needed.

Thank you for responding I tried the following code

var base64Data = context.getVariable("request.content['event']");
var compressData = atob(base64Data);

context.setVariable("requestXML", compressData);

and got the following error. Not sure what i am missing

Execution of JavaScript-2 failed with error: Javascript runtime error: "ReferenceError: "atob" is not defined. (JavaScript-2.js:4)"

Two things

  • atob is not available in the JS Callout.  The runtime is Rhino, not v8. You won't have all the JS builtins. Check the Apigee documentation for details. There are other ways to base64-decode data in Apigee.
  • But it seems like you have a data stream which is gzipped-then-base64encoded and you want to decode it.  Base64-decoding only gets you to a gzipped (compressed) data stream.  AFAIK there is no way using Apigee builtin capabilities to gunzip a data stream in Apigee.  You could write a Java callout to do it, easily. 

What are you really trying to do?  What is the "requestXML" thing?  Are you trying to decode a gzipped-then-base64-encoded SAML Assertion? If so, this pre-built Java callout may help you:  Apigee Java SAML Decoder. It does the base64-decode-then-gunzip thing. In the case of a data stream like PFJlc3BvbnNlIERlc3Rp.... It produces an XML assertion. 

Thank you Dino for confirming. I was hoping we could gunzip in Apigee via a Javascript maybe. I will checkout the Java callout.