Send Rest Client requests through an Http Client Handler in this app
Enable this rule when you want each app to send its HTTP calls from its own extension, regardless of the dependency structure.
Codeunit "Rest Client" does not send requests itself. It passes every request to an implementation of the "Http Client Handler" interface. When no handler is given, it uses the System Application’s own "Http Client Handler" codeunit, which calls HttpClient.Send. When the app passes a handler from a dependency app, that app’s codeunit sends the request. Either way, the HTTP call is made from another extension, not from the app that built the request.
This matters for the Allow HttpClient Requests setting on the Extension Settings page. The setting “specifies whether the runtime should allow this extension to make HTTP requests through the HttpClient data type when running in a non-production environment.” Microsoft Learn states that outbound HTTP calls are blocked by default and must be approved for each extension, including library apps. A blocked call fails with The request was blocked by the runtime to prevent accidental use of production services. When a production environment is copied to a sandbox, outbound HTTP calls are blocked in the copy until the setting is turned on for each extension.
When the System Application’s default handler sends the request, the setting of the app that built the request does not decide whether the call goes out. Turning the setting on for the System Application allows the calls of every app that relies on the default handler. Implement "Http Client Handler" in the app and pass it to the Rest Client, so the app’s own setting governs its calls.
The handler in the app has two more benefits. The outgoing web service request telemetry for the call belongs to the app’s own extension. Tests can replace the handler to substitute a response instead of calling the real service.
Example
codeunit 50100 "Exchange Rate Sync" // Send Rest Client requests through an Http Client Handler in this app [AC0033]
{
procedure GetRates(CurrencyCode: Code[10]): JsonToken
var
RestClient: Codeunit "Rest Client";
begin
exit(RestClient.GetAsJson('https://api.example.com/rates/' + CurrencyCode));
end;
}Add a codeunit that implements "Http Client Handler" and pass it to RestClient.Initialize:
codeunit 50110 "Exchange Rate Http Handler" implements "Http Client Handler"
{
Access = Internal;
procedure Send(CurrHttpClientInstance: HttpClient; HttpRequestMessage: Codeunit "Http Request Message"; var HttpResponseMessage: Codeunit "Http Response Message") Success: Boolean;
var
ResponseMessage: HttpResponseMessage;
begin
Success := CurrHttpClientInstance.Send(HttpRequestMessage.GetHttpRequestMessage(), ResponseMessage);
HttpResponseMessage := HttpResponseMessage.Create(ResponseMessage);
end;
}
codeunit 50100 "Exchange Rate Sync"
{
procedure GetRates(CurrencyCode: Code[10]): JsonToken
var
RestClient: Codeunit "Rest Client";
HttpHandler: Codeunit "Exchange Rate Http Handler";
begin
RestClient.Initialize(HttpHandler);
exit(RestClient.GetAsJson('https://api.example.com/rates/' + CurrencyCode));
end;
}RestClient.Create(HttpHandler) is equivalent. It returns the initialized Rest Client, which suits code that assigns the client in one statement:
RestClient := RestClient.Create(HttpHandler);Initialize(), Initialize(HttpAuthentication), Create() and Create(HttpAuthentication) all use the default handler, and so does a Rest Client that is used without being initialized. AC0033 only checks that the app contains a handler implementation. It does not check that each Rest Client receives it. AC0035
checks that each Rest Client variable receives it.
When the diagnostic is reported
- An object declares a global variable, local variable, parameter or return value of type
Codeunit "Rest Client". Locals in triggers of fields, actions, controls, report data items and XMLport elements count. - No codeunit in the same app implements
"Http Client Handler". - The Rest Client codeunit is matched on both its ID (2350) and its name.
- One diagnostic is reported per object, on the object name, regardless of how many Rest Client variables the object declares.
Exception
Test codeunits (Subtype = Test or TestRunner) and obsolete objects are not reported.
Why the rule is disabled by default
A shared base app that sends the HTTP calls for the apps that depend on it is a valid design. In that design the base app’s setting governs the calls of all its dependent apps. AC0033 only counts handler implementations in the app being compiled, so it does not see a handler in a dependency and would report every dependent app.
Enable the rule
Add AC0033 to your project’s ruleset file. Use Info to show it as a suggestion, or Warning to make it stand out:
{
"id": "AC0033",
"action": "Warning",
"justification": "Each app sends its HTTP calls from its own extension"
}
AC0034
applies the same check to Codeunit Telemetry and Codeunit "Feature Telemetry".
See also
- Call external services with the HttpClient data type on Microsoft Learn
- Codeunit “Rest Client” on Microsoft Learn
- Interface “Http Client Handler” on Microsoft Learn
- Prefer the Rest Client module over the native HttpClient on GitHub Discussions