Forwarding Api Proxy

There’s a mobile app that I’m working on that will be performing complex queries against a database that is exposed via an OData api. One small hitch is that the mobile app is not allowed to access the OData api directly, but I can implement a proxy that will have access to the OData api and is deployed in a place that the mobile app can access. While I do not want to try and consume the request from the mobile app, I would like the ability to perform some logic before and after the request is forwarded in order to allow for either logging or validation purposes.

What I have created is a simple ASP.NET api that forwards requests to a configured host and returns the results. Any path or query string parameters are passed on to the new host just as they are received. It supports the Http Methods GET, PUT, POST, PATCH, and DELETE.

The desired target host is configured in the web.config file AppSettings:

The ProxyController only has one endpoint that is used to catch all requests and forward them to the configured host, returning the results.

In order to allow custom logic before or after the call to the OData api, there is a CustomFilter that is added to the ProxyController. It contains two methods that allow you to perform logic before the request is forwarded, and before results are returned to the caller:

When using this proxy, the user (or mobile app, in this instance) is not aware that any forwarding has happened, and it appears that all path and query parameters have been handled by the highly complex api that I built! Notice how the URL in the browser still shows the url for my Proxy api:

When the results are coming from the sample OData service at http://services.odata.org/V3/Northwind/Northwind.svc:

You can find the code for this project on my GitHub account here: https://github.com/danksalot/ApiProxy

Leave a Reply

Your email address will not be published. Required fields are marked *