The tendency to move from assembling HTML pages on the server side to building them on the client side is not even a trend, but a trend. Magento 2, keeping up with the times to the best of its capabilities, is also trying to be in trend , carrying data processing and presentation. How can a simple developer look at the "clean" data, if their presentation is rendered far enough? There are many other good solutions (starting with tcpdump ), and there is a swagger .
Swagger is fairly well integrated into Magento 2 (unless it occurred to you to make a stupid idea to change the code for the "default" showcases). All you need to start looking at the “clean” data in Magento 2 from the perspective of a remote application is to use the Swagger integrated into it. In this article, I do not consider the features of using Swagger itself, but simply give an example of how to use Swagger integrated in Magneto 2 in anonymous user mode and in authenticated user mode.
I deployed a simple Magento module at http://module.sample.flancer32.com/ . To start using Swagger in anonymous user mode, go to http://module.sample.flancer32.com/swagger First, the Swagger application itself loads (quickly), and then the description of the web services implemented in Magento is loaded (slowly) :
The Magento services JSON schema can be pulled out by the link itself: http://module.sample.flancer32.com/rest/default/schema?services=all
After parsing the scheme, Swagger generates a list of services available to an anonymous client:
and allows not only to study the services themselves in a human-readable form:
... but also form requests:
... and analyze the answers:
First you need to do in Magento a separate user (using an administrative account to access services is not a good idea):
and assign access rights to it (User Roles):
Rather, the role is first made, and then, when created, it is assigned to the new user. Giving all rights through a role is also not a good idea, but then I am not perfect either. Therefore, in our example, the user "swagger" is assigned the role of "Swagger", which is granted access rights to any resources.
Authentication via the web-interface is not provided, someday someone might finish this moment in Magento 2, but so far you have to authenticate manually: through your favorite universal REST API client, through program code or through "curl":
curl -X POST "http://module.sample.flancer32.com/rest/V1/integration/admin/token" \ -H "Content-Type:application/json" \ -d '{"username":"swagger", "password":"0jul4san0lRLKjYQpCqx"}'
All you need to do is send a specific JSON text with authentication parameters to a specific address (without forgetting the header with the content type of the request). In response, the session code will come to access the limited services:
"f6j0arlp29vx8i3av9l3eu5sag2pb5qy"
The code will come in quotes, but you need to use it without.
Well, now, having the code, we enter it in the appropriate field:
... and get access to an already wider list of services. For example, to this:
Further work with the services is no different from working in anonymous mode. The session key for authorization is added by Swagger automatically:
Swagger is a great tool, wonderfully integrated into the wonderful Magento 2 platform. It’s a pleasure to develop web services with such tools.
Source: https://habr.com/ru/post/317770/
All Articles