API Connect

API Connect

Join this online group to communicate across IBM product users and experts by sharing advice and best practices with peers and staying up to date regarding product enhancements.

 View Only
  • 1.  Format of invoke response body in APIC - Why Buffer instead of JSON?

    Posted 3 days ago

    I have a setup where:

    • I have both a proxy API and a backend API.

    • The backend API is configured to return Content-Type: application/json.

    • The proxy calls the backend API using an invoke policy.

    When I inspect the invoke response object in the proxy (e.g. context.message.body.write(JSON.stringify(context.get('tokenResponse')));), the body property shows as: "body": {"type":"Buffer","data":[123,34,97,99,99,101,115,115,95,116,111,107,101..}" Instead of a parsed JSON object.

    My questions:

    1. In what format does APIC store the invoke response object?

    2. Why is the JSON response represented as a Buffer?

    3. Is using a Parse policy the only recommended way to convert the buffer to a JSON object, or is there a direct way in GatewayScript?

    I want to understand the expected behavior and best practice for handling backend JSON responses in proxy APIs.

    Thanks in advance for any guidance!



    ------------------------------
    Mounika Nayikoti
    ------------------------------


  • 2.  RE: Format of invoke response body in APIC - Why Buffer instead of JSON?

    Posted 2 days ago

    Hi Mounika,

    The invoke policy will always return a buffer with the exact contents that were on the wire.  It is up to the API to decide if it wants/needs the buffer parsed or not.  Parsing does impact the whitespace for example which could impact any type of hashing which would be one reason to not parse it.  You can use either the parse policy, or in GatewayScript, you'll read the message.body as a buffer and can used JSON.parse(theBuffer) to parse it within your code.  If you need the parsed payload for a subsequent policy, the parse policy would be recommended as it would be more performant than the GatewayScript, but if using GatewayScript, you could also do context.set to place the parsed JSON into your body variable.

    Regards,

    Steve Linn



    ------------------------------
    Steve Linn
    Senior Consulting I/T Specialist
    IBM Retired
    ------------------------------



  • 3.  RE: Format of invoke response body in APIC - Why Buffer instead of JSON?

    Posted 2 days ago

    Hi Steve,

    Thank you for your detailed response - it really cleared my doubt about why the invoke response is stored as a buffer.

    Just to confirm, in scenarios where we need to extract specific fields from the response body (for example, fetching a token value from the first invoke response and sending it in another invoke call), parsing the buffer to JSON would have a performance impact, correct?

    I'm trying to understand whether this cost is negligible for small payloads, or if we should look at alternative extraction approaches to optimize performance.

    Thanks again for your guidance!



    ------------------------------
    Mounika Nayikoti
    ------------------------------



  • 4.  RE: Format of invoke response body in APIC - Why Buffer instead of JSON?

    Posted 2 days ago

    Hi Mounika,

    As you point out, the size of the payload being parsed would be proportional to amount of time required to parse it, but for what would be considered standard payloads it should be nominal.  I'd agree with you that not parsing it is the best approach unless you really need to extract something for some subsequent policy.  I'd think any "alternative approach" would involve string manipulation which could prove to be error prone depending upon your search criteria and would probably be as costly in cycles spent versus parsing it in the first place.

    Best Regards,

    Steve Linn



    ------------------------------
    Steve Linn
    Senior Consulting I/T Specialist
    IBM Retired
    ------------------------------