Saturday, July 26, 2025

#1077 OCI API Gateway & OIC - Multiple Backend support

OCI API Gateway can be used to manage access to your your integration apis. This post goes into a bit more details on the mechanics of such, for different types of integrations. The main example covers routing to different integrations, based on Content-Length. The use case is that of processing a file; files over 5MB are classed as large files, those <= 5MB are classed as small files.

I begin with a simple German version of Hello World, to get us started.

The initial setup of the OCI Gateway is simple. For those who want a step by step, check out my post here.

Now to my integrations - 

First HalloWelt - 

Let's expose HalloWelt via the gateway.

That's it - 

Pick up the endpoint, once created - 

Test from Postman -


Now to the next integration - 

This integration pushes a file to OIC file server.

Now to test this from Postman, firstly I take the curl representation of the request - 

and I drop it into Postman. I could also have copied the endpoint url from here -


 




That worked fine - let's look at the postman console - 

Note the Content-Type header.

Now I expose this via the api gateway. For this to work, I need to set the following in the route -


As you can see, I'm explicitly setting the headers. In fact, I'm passing on what I receive from postman. Note the syntax for referring to the Content-Type - ${request.headers[Content-Type]}.

I now run this from postman - 


Let's remove the Header transformation - 





 






I get the following error in postman -

Exception while processing Trigger request for </ic/api/integration/v2/flows/rest/project/AA_LARGEFILEPROCESS/WRITELARGEFILETO_V2/1.0/file>. Error Message : Unsupported media type in the request 


Let's include some more logic here - if the size of the file <= 5 MB then route to integration A, else route to integration B. As you can see, I've added a new integration to my project.

I will cover 2 approaches here -

  1. use of an X header
  2. interrogate the Content-Length header. Here we need to interrogate the http header Content-Length. We could refer to this as ${request.headers[Content-Length]}.

Use of an X-Header

Mine is called X-Large-File, with 2 possible values, Y & N.
 

I create a new deployment and add the following rule to the route -



Test from postman, with the X-Large-File header set to N.

Now test with the header set to Y.


Checking the Content-Length value

Essentially we want to implement the following check - if content-length > 5 MB then invoke the large file processing integration, else invoke the small file processing integration.

Net, net - ${request.headers[Content-Length]} > 5242880 (bytes).

Unfortunately, we cannot implement this directly in the api gateway. We need to implement a multi argument authorizer function, to take the value of content-length, cast it and perform the required comparison. Then we can set an auth context, such as backend = large_payload_service or small_payload_service. That auth context can then be used as the selector.

Here's the python code -

import io
import json
import logging

# comment

def handler(ctx, data: io.BytesIO = None):
    body = json.loads(data.getvalue())
    content_length_str = body.get('data', {}).get('Content-Length', '0')
    content_length = int(content_length_str)
   
    if content_length > 5 * 1024 * 1024:  # 5MB
        response =  {"active": True,
                     "context":
                      {
                        "backend": "large_payload_service"
                      }
                    }
    else:
        response =  {"active": True,
                     "context":
                      {
                        "backend": "small_payload_service"
                      }
                    }

    json_response = json.dumps(response)
    return json_response
       

Now that we have the function, let's use it!

Some things to note

OCI API Gateway has a limit of 20MB on request body size.

Ensure the name you use as the argument in the Authorization call is matched in the python code - 

This is used in python - 

content_length_str = body.get('data', {}).get('Content-Length', '0')
 
The first execution of the function includes ramp up time, so expect this first invoke to take a little longer.

Log files for OCI API Gateway and Functions 

It's a good idea to enable logs, let's start with our deployment in OCI API Gateway -

As you can see, 2 logs - access and execution.

Testing

Small file test:

Large file test:

Viewing the Logs

OCI API Gateway, execution log - 

Functions Log -