Handle the response of an external web service
@creatio-devkit/common includes the HttpClientService service that sends HTTP requests.
Detailed example: Handle the response of an external web service on a page.
To handle the response of an external web service:
-
Add the dependencies. Instructions: Display the value of a system variable (similarly to step 2). Instead of the
SysValuesServiceservice, use theHttpClientServiceservice that sends HTTP requests. -
Add an attribute. Instructions: Set up the field display condition (step 2).
-
Bind an attribute to the label. Instructions: Set up the field display condition (similarly to step 3). Instead of the
visibleproperty, use thecaptionproperty that handles the text displayed in the element. -
Implement the base request handler.
-
Go to the
handlersschema section. -
Add a custom implementation of the
crt.HandleViewModelInitRequestbase request handler.- Create an instance of the HTTP client from
@creatio-devkit/common. - Specify the URL to retrieve required data. If a web service request is sent using a non-absolute path (without
https://orhttp://prefixes), this is a request to an internal Creatio web service. In this case, Creatio automatically adds the address of the current app to the link. - Send a
GETrequest. - Retrieve required data from the response and specify data in the corresponding attribute.
- Create an instance of the HTTP client from
View an example of a
crt.HandleViewModelInitRequestrequest handler that sends a request to thehttps://SomeUrlweb service, receives thesomeValueparameter from the response body, and specifies the parameter in theSomeAttributeattribute, below.handlers schema sectionhandlers: /**SCHEMA_HANDLERS*/[{request: "crt.HandleViewModelInitRequest",/* Custom implementation of a system request handler. */handler: async (request, next) => {/* Create an instance of the HTTP client from "@creatio-devkit/common." */const httpClientService = new sdk.HttpClientService();/* Specify the URL to retrieve required data. */const endpoint = "https://SomeUrl";/* Send a GET request. The HTTP client converts the response body from JSON to a JS object automatically. */const response = await httpClientService.get(endpoint);/* Retrieve the "someValue" parameter from the response body and specify it in the "SomeAttribute" attribute. */request.$context.SomeAttribute = response.body.someValue;/* Call the next handler if it exists and return its result. */return next?.handle(request);}}]/**SCHEMA_HANDLERS*/, -