Global routes with rules

If you want to serve the same responses based on the same rules for all or part of your endpoints, you can create global routes using the fallback mode and a wildcard path. This is useful if you want to protect all your endpoints by checking if an Authorization header is present or if you want to verify that all your requests contain a specific property in their body.

Three parts are required to create a global route:

 1. Create a wildcard route

To create a global route, you first need to create a new HTTP route that will match all the endpoints you want to protect. To do so, create a new route and select "All methods" in the method dropdown:

wildcard route on all methods

In this example, the wildcard route is placed above the GET /users route, which is important as it will be evaluated first and catch all the requests. You can create it first or move it up in the routes list with a drag and drop.

 2. Create your global responses and rules

The second step is to create one or more responses with rules. For example, you can create a response checking that the request contains an Authorization header and returning a 401 error if's not present. To follow this example, use the existing response or create a new one and set the status code to 401:

create response with status code 401

Add a new rule to this response by clicking on the "add rule" button and selecting the "Header" rule type. Then, set the rule to check that the Authorization header is null as we want to return a 401 error if it is not present:

create rule to check that the Authorization header is null

 3. Activate the fallback mode

Once your route is created and your responses customized, activate the fallback mode by clicking on the "fallback" icon next to the response list:

Activate fallback mode

This will automatically pass the request to the next route when none of the responses' rules match.