Construct REST requests with AL HTTP types
A REST request combines an HTTP method, an absolute URI, request headers, and optional content. AL separates these concerns across HTTP types so that your code can match the external service contract explicitly.
The short procedures in the conceptual units are teaching fragments, not additional objects to add beside the complete exercise codeunit and page. Required service endpoint URIs remain absolute; documentation links use site-relative paths.
Select the HTTP types
The following AL types work together during an outbound request.
| AL type | Responsibility |
|---|---|
HttpClient |
Sends a request and receives an HttpResponseMessage. |
HttpRequestMessage |
Holds a request-specific method, URI, headers, and content. |
HttpContent |
Holds the request or response body and its content headers. |
HttpHeaders |
Holds request, content, or response headers. |
HttpResponseMessage |
Holds the response status, reason phrase, headers, and content. |
Use the convenience methods on HttpClient when the operation maps directly to GET, POST, PUT, PATCH, or DELETE. For example, Get needs a URI and a response variable. Post and Put also receive HttpContent. Use HttpRequestMessage with HttpClient.Send when you need one object that describes the method, URI, request headers, and body.
Use convenience methods only when they are available in your target runtime. For example, HttpClient.Patch requires runtime 14.0 or later; HttpRequestMessage with Send provides the request-specific alternative.
The HTTP method communicates intent:
GET retrieves a representation without changing it.
POST submits data and commonly creates a resource or starts an operation.
PUT replaces a resource at a known URI.
PATCH changes part of a resource.
DELETE removes a resource.
The endpoint’s contract determines the correct method. A method that compiles in AL can still produce status 405 if the endpoint doesn’t support it.
Define the method and URI
For a direct GET request, pass the absolute URI to HttpClient.Get and capture its Boolean return value.
local procedure GetPost(PostId: Integer; var Response: HttpResponseMessage): Boolean
var
Client: HttpClient;
Endpoint: Text;
begin
Endpoint := StrSubstNo('https://jsonplaceholder.typicode.com/posts/%1', PostId);
exit(Client.Get(Endpoint, Response));
end;
The Boolean result reports whether the Business Central platform completed the call. It doesn’t report whether the service returned a successful HTTP status.
For request-specific control, configure an HttpRequestMessage and pass it to HttpClient.Send.
local procedure SendRequest(Method: Text; Endpoint: Text; var Response: HttpResponseMessage): Boolean
var
Client: HttpClient;
Request: HttpRequestMessage;
RequestHeaders: HttpHeaders;
begin
Request.Method(Method);
Request.SetRequestUri(Endpoint);
Request.GetHeaders(RequestHeaders);
RequestHeaders.Add('Accept', 'application/json');
exit(Client.Send(Request, Response));
end;
The URI must be absolute unless you first configure a base address on HttpClient. Keep environment-specific endpoints in configuration rather than distributing different source code for each environment. If a URI contains a secret, use HttpRequestMessage.SetSecretRequestUri instead of storing that URI as plain Text.
Place headers in the correct collection
Request headers describe the request. Content headers describe the body. The distinction affects how the remote service interprets the message.
Common request headers include:
Accept, which identifies response media types that the caller can process.Authorization, which carries authentication information.Correlation headers, which connect a request to logs across systems.
Common content headers include:
Content-Type, which identifies the body format.Content-Encoding, which identifies an encoding applied to the body.
HttpContent starts with Content-Type: text/plain; charset=utf-8. A JSON endpoint commonly requires application/json, so remove the default value before adding the required value.
local procedure CreateJsonContent(JsonText: Text; var Content: HttpContent)
var
ContentHeaders: HttpHeaders;
begin
Content.WriteFrom(JsonText);
Content.GetHeaders(ContentHeaders);
if ContentHeaders.Contains('Content-Type') then
ContentHeaders.Remove('Content-Type');
ContentHeaders.Add('Content-Type', 'application/json');
end;
Adding Content-Type to the request-header collection can cause a validation error or a service response such as 415 Unsupported Media Type. Avoid duplicate headers because the platform can reject a request before sending it.
Protect authentication values
The exercise endpoint doesn’t require authentication. Production endpoints commonly require an OAuth token, API key, or client certificate. Treat those values as configuration and secrets rather than literals in source code.
AL provides SecretText overloads for sensitive values in types such as HttpHeaders, HttpContent, and HttpClient. A secret authorization value can be added without converting it to plain text.
local procedure AddAuthorizationHeader(var Headers: HttpHeaders; AccessToken: SecretText)
begin
Headers.Add('Authorization', AccessToken);
end;
The value supplied to the method must already contain the authorization scheme required by the service, such as a bearer scheme. Authentication logic should remain separate from request construction so that you can change credential acquisition without rewriting JSON serialization or response parsing.
For the complete API surface, see HttpClient data type and Call external services with the HttpClient data type.