Using Swagger for application troubleshooting
The next tool we can use to test and debug our application is Swagger. We already got acquainted with it earlier; as you remember, in it, you can see all the endpoints of the application being developed, run them, and get the result. Let's look at this in more detail.
Authorization in Swagger
First of all, remember that Middleware Token Auth is enabled by default, which controls access to the endpoint for most endpoints. It denies access for users who have not been authorized or even passed it but do not have the appropriate access rights (for example, they belong to the Users group, and the endpoint is intended exclusively for Admins).
The necessary authorization token can be found using the Developer Tools. All you need to do is log in to the published web application and then fulfill any request. The token will be used in the Request Headers. For example, you can see it on the screenshot a little higher at the moment when we made a GET request to get data about countries.
Let's see how you can get it directly in Swagger. To do this, find the endpoint that is responsible for authorization (Auth group, POST /Auth/ endpoint) and click the "Try it out" button. This endpoint takes two parameters, Login, and Password. Specify them and click Execute to send the request. By the way, here, it can also be useful to use the Developer Tools; you can open them for a more detailed study of the request and response.
The response will receive the required token, as well as detailed information about the user and his sessions.
You can see that the response received is not much different from the one we already saw in the Developer Tools. At the same time, Swagger has an undeniable advantage in that it makes it possible to test any requests and launch endpoints with the necessary parameters in a convenient form. But before proceeding with such testing, let's click Authorize and use the received token to confirm authorization.
Sending requests using Swagger
Now let's send a request through Swagger to get data on the first city that comes across, with a population of more than 5 million people and the word "World" in the description. To do this, open the GET /city/ endpoint and fill in the required parameters:
- _limit = 1 (only one city needed)
- population_from = 5000000 (population value should be checked)
- _search_in = info (search only in the info field)
- _search = World (looking for the word World)
The received response shows that the request was executed correctly. We received information about one such city (we see that count = 3, which means that the database contains information about three suitable cities) and did not receive any data about the country in which it is located (“country”: null), since the query did not need to present data from related tables (if necessary, it was worth using the _with parameter).