Multiple middleware and chaining

In the previous section, we built a single middleware to perform an action before or after a request hits the handler. It is also possible to chain a group of middleware. In order to do that, we should follow the same closure logic as in the preceding section. Let's create a cityAPI program for saving city details. For simplicity's sake, the API will have one POST method, and the body will consist of two fields: city name and city area.

Let's us think about a scenario where a client is only allowed to send a JSON Content-Type request to an API. The main function of the API is to send a response to the client with a UTC timestamp cookie attached to it. We can add that content check in the middleware.

The functions ...

Get Hands-On RESTful Web Services with Go - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.