With Azure API Management (API M) you can turn any API into a RESTful API. To demonstrate, this post will detail the steps to transform a simple web service into a RESTful API without any code change or refactoring.
To start I have a simple ASP.NET MVC web service that exposes one endpoint "api/action" with two possible actions "GetBook" and "CreateBook" passed in as JSON objects - nothing RESTful here. Rather than refactor my code into something RESTful I am going to simply use API M to wrap a RESTful interface around my existing API backend, which can be left as is with no code change necessary. This pattern works great for modernising existing APIs that may be too complex and time consuming to change.
To find the code for my simple web service view my GitHub here. Feel free to follow along with these steps, to do so you will need a:
- Azure Subscription
- Azure API M instance
- Hosting platform for your API (I am using Azure Web Apps for this as I can simply deploy from my visual studio environment)
Here is what a call to my existing web service looks like on postman. My endpoint URL is '/api/action' and I am passing in the action I want to execute in a HTTP POST as JSON. My backend will then parse that, get the result and simply send a confirmation of the action triggered as seen here:
The first step in creating a RESTful API is to define the contract of the actions, responses and requests in the OpenAPI specification. I have included mine on GitHub here.
For details on how to write an OpenAPI Spec I followed API Handymans series. It does a great job in going through the process step by step.
Once this specification has been defined I can upload this to API M and start playing around with it.
To upload your OpenAPI Spec to API M click on the API tab in the main blade of your API M instance (please note that the functionality of the publisher portal has been moved to the Azure portal).
Click the OpenAPI specification option to upload your spec as a file or URL.
Here are the options that I filled in:
Once uploaded we can start to tweak our API in the portal. To allow our RESTful calls to hit our backend service in the right format we need to do some URL translation on the fly. To allow my backend to understand the incoming requests I need to change the HTTP methods from GET to POST, changing the URL from /books to /api/action and add in a JSON object in the body message.
To implement these changes I have added some policies to my inbound requests as seen here.
To add and configure these select edit.
Here I have changed the HTTP method from GET to POST, rewritten the incoming URL from /books to /api/action and added in a body as JSON with an input parameter of "GetBook". Now this request is in the right format to be executed by my backend service. This would be a similar process for the "CreateBook" operation.
To test this is working as expected I can go ahead and test the API from API M itself under the Test tab. Select the GET books operation and try it out.
As you can see we have successfully turned an old non-REST API into RESTful with no code change, just simple tweaks in Azure API M.