Breyta

Deila með


HttpContent Data type

Version: Available or changed with runtime version 1.0.

Represents an HTTP entity body and content headers.

Instance methods

The following methods are available on instances of the HttpContent data type.

Method name Description
Clear() Sets the HttpContent object to a default value. The content contains an empty string and empty headers.
GetHeaders(var HttpHeaders) Gets the HTTP content headers as defined in RFC 2616.
IsSecretContent() Returns if the content is secret. If it is secret it can be read only as a SecretText.
ReadAs(var SecretText) Reads the content into the provided secure text.
ReadAs(var Text) Reads the content into the provided text.
ReadAs(var InStream) Reads the content into the provided text.
WriteFrom(Text) Sets HttpContent content to the provided text or stream.
WriteFrom(SecretText) Sets HttpContent content to the provided SecretText.
WriteFrom(InStream) Sets HttpContent content to the provided text or stream.

An instance of HttpContent encapsulates the body and the associated headers of an HTTP request that will be sent to a remote endpoint or that is being received from a remote endpoint. The HttpContent data type is a value type. This means that when assigning an instance of HttpContent to a variable, a copy will be created.

Content headers

By default, the content headers contain a header for 'Content-Type', namely 'text/plain; charset=utf-8'. This is important to adjust if the endpoint you're calling expects a different content type, for example, multipart/form-data or application/json.

The following AL code examples illustrate ways to change the content header.

    local procedure SendRequest(HttpMethod: Text[6]) ResponseText: Text
    var
        Client: HttpClient;
        Content: HttpContent;
        ContentHeaders: HttpHeaders;
    begin
        Content.GetHeaders(ContentHeaders);

        // If you want to set all the headers yourself, do it this way
        ContentHeaders.Clear();
        ContentHeaders.Add('Content-Type', 'multipart/form-data;boundary=boundary');

        // from here on, the method must deal with calling out to the external service. 
    end;
    local procedure SendRequest(HttpMethod: Text[6]) ResponseText: Text
    var
        Client: HttpClient;
        Content: HttpContent;
        ContentHeaders: HttpHeaders;
    begin
        Content.GetHeaders(ContentHeaders);

        // In this example, you just change the headers you need
        if ContentHeaders.Contains('Content-Type') then ContentHeaders.Remove('Content-Type');
        ContentHeaders.Add('Content-Type', 'multipart/form-data;boundary=boundary');

        // from here on, the method must deal with calling out to the external service. 
    end;

When calling an endpoint, you get a response back from the service you called. The response is saved in the data type HttpResponseMessage, where you can parse it to use the information in your app. The service call itself might not succeed, so make sure that you check the HTTP status code in your AL code. In the case of setting the wrong content type header, you'll typically receive the 415 Unsupported Media Type return code. This return code can also be due to the request's Content-Encoding header, or if the data you send doesn't match what the endpoint expects.

Example

The following example illustrates how to use the HttpContent type to send a simple POST request containing JSON data.

codeunit 50110 MyCodeunit
{
    procedure MakeRequest(uri: Text; payload: Text) responseText: Text
    var
        client: HttpClient;
        request: HttpRequestMessage;
        response: HttpResponseMessage;
        contentHeaders: HttpHeaders;
        content: HttpContent;
        IsSuccessful: Boolean;
    begin
        // Add the payload to the content
        content.WriteFrom(payload);

        // Replace the default content type header with a header associated with this request
        content.GetHeaders(contentHeaders);
        contentHeaders.Clear();
        contentHeaders.Add('Content-Type', 'application/json');

        // Assigning content to request.Content will actually create a copy of the content and assign it.
        // After this line, modifying the content variable or its associated headers will not reflect in 
        // the content associated with the request message
        request.Content := content;

        request.SetRequestUri(uri);
        request.Method := 'POST';

        IsSuccessful := client.Send(request, response);

        if not IsSuccessful then begin
            // handle the error
        end;

        if not response.IsSuccessStatusCode() then begin
            // handle the error
        end;

        // Read the response content as json.
        response.Content().ReadAs(responseText);
    end;
}

Get Started with AL
Developing Extensions