Proxying a Route:
Alright! The proxy is up. Let’s learn how to proxy a route.
First, an important note:
It would be ideal for every subdomain mentioned herein to have a DNS entry pointed at the server running API Fortress. Our suggestion is, wherever possible, to introduce a wildcard entry. That is to say, if API Fortress is running at ‘apif.example.com’, the wildcard entry of ‘*.apif.example.com’ would point at the same IP address and allow every prepended domain to reach the same server.
In order to proxy an API route, we need to send the following request to the proxy via HTTP (again, this can be done via cURL or your HTTP client of choice):
curl -v -XPOST -d "name=apif" -d "upstream_url=http://demoapi.apifortress.com" -d "hosts=proxy-demoapi.apif.example.com" apif.example.com:8001/apis
That’s quite the route to unpack. Let’s take a look!
name: the name of the API profile
upstream_url: the origin URL (the destination that we’re passing through the proxy on our way to)
hosts: A list of hosts that will trigger this API profile (the URL(s) that will trigger this proxied response)
So, we’re sending a post to apif.example.com:8001/apis with headers defining a name, an upstream URL and hosts. The result is a profile of a proxied API. The name
of the profile is “apif.” The upstream URL is “http://demoapi.apifortress.com” and the host for the profile is “proxy-demoapi.apif.example.com”
Naturally, when you do this yourself, you’ll be replacing “apif.example.com” with the URL of your API Fortress instance.
The last step in the setup phase would be to test the actual proxied route itself. To do so, execute the following command in the command line or send it with the HTTP client of your choice:
curl -v -H 'key:ABC123' http://proxy-demoapi.apif.example.com:8000/api/retail/product
Here, the ‘header:value’ string would be replaced with any required header key/value pairs that you need to submit. The URL would be replaced with the URL previously defined as the host, appended with port 8000, and then followed by any necessary routing. Our expected response should match the response of the endpoint that we’re proxying, provided we’re passing the correct headers.
NEXT – Step 3: Recording a Mock Endpoint