{"title":"HttpContent data type","url":"https://jonas-paprotka.de/docs/dev-itpro/developer/methods-auto/httpcontent/httpcontent-data-type/","by":"JOP-AL","license":"Copyright (c) Microsoft Corporation. CC BY 4.0, https://creativecommons.org/licenses/by/4.0/; code samples MIT. From https://learn.microsoft.com/en-us/dynamics365/business-central/dev-itpro/developer/methods-auto/httpcontent/httpcontent-data-type, MicrosoftDocs/dynamics365smb-devitpro-pb at 828b6ee (2026-11-19). Changed: includes filled in, links to this site, and for agents shortened without dropping a fact.","credit":"Microsoft Corporation, CC BY 4.0","origin":"https://learn.microsoft.com/en-us/dynamics365/business-central/dev-itpro/developer/methods-auto/httpcontent/httpcontent-data-type","source":{"name":"Business Central developer and IT pro docs","repo":"https://github.com/MicrosoftDocs/dynamics365smb-devitpro-pb","commit":"828b6ee72fcd75755627cd6011f3d20899217ab2","date":"2026-11-19"},"checked":"2026-11-19","description":"Represents an HTTP entity body and content headers.","date":"2026-10-01","meta":{"topic":"reference"},"outline":[{"level":2,"title":"Instance methods","slug":"instance-methods","at":112,"chars":1390},{"level":2,"title":"Content headers","slug":"content-headers","at":1502,"chars":2031},{"level":2,"title":"Example","slug":"example","at":3533,"chars":1501},{"level":2,"title":"Related information","slug":"related-information","at":5034,"chars":117}],"text":"> Version: _Available or changed with runtime version 1.0._\nRepresents an HTTP entity body and content headers.\n## Instance methods\nThe following methods are available on instances of the HttpContent data type.\nMethod name|Description\nhttpcontent-clear-method|Sets the HttpContent object to a default value. The content contains an empty string and empty headers.\nGetHeaders(var HttpHeaders)<httpcontent-getheaders-method>|Gets the HTTP content headers as defined in RFC 2616.\nhttpcontent-issecretcontent-method|Returns if the content is secret. If it is secret it can be read only as a SecretText.\nReadAs(var SecretText)<httpcontent-readas-secrettext-method>|Reads the content into the provided secure text.\nReadAs(var Text)<httpcontent-readas-text-method>|Reads the content into the provided text.\nReadAs(var InStream)<httpcontent-readas-instream-method>|Reads the content into the provided text.\nhttpcontent-writefrom-text-method|Sets HttpContent content to the provided text or stream.\nhttpcontent-writefrom-secrettext-method|Sets HttpContent content to the provided SecretText.\nhttpcontent-writefrom-instream-method|Sets HttpContent content to the provided text or stream.\nAn 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.\nThe HttpContent data type is a value type. This means that when assigning an instance of HttpContent to a variable, a copy will be created.\n## Content headers\nBy 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.\nThe following AL code examples illustrate ways to change the content header.\n```al\n    local procedure SendRequest(HttpMethod: Text[6]) ResponseText: Text\n    var\n        Client: HttpClient;\n        Content: HttpContent;\n        ContentHeaders: HttpHeaders;\n    begin\n        Content.GetHeaders(ContentHeaders);\n\n        // If you want to set all the headers yourself, do it this way\n        ContentHeaders.Clear();\n        ContentHeaders.Add('Content-Type', 'multipart/form-data;boundary=boundary');\n\n        // from here on, the method must deal with calling out to the external service.\n    end;\n```\n```al\n    local procedure SendRequest(HttpMethod: Text[6]) ResponseText: Text\n    var\n        Client: HttpClient;\n        Content: HttpContent;\n        ContentHeaders: HttpHeaders;\n    begin\n        Content.GetHeaders(ContentHeaders);\n\n        // In this example, you just change the headers you need\n        if ContentHeaders.Contains('Content-Type') then ContentHeaders.Remove('Content-Type');\n        ContentHeaders.Add('Content-Type', 'multipart/form-data;boundary=boundary');\n\n        // from here on, the method must deal with calling out to the external service.\n    end;\n```\nWhen 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.\n## Example\nThe following example illustrates how to use the HttpContent type to send a simple POST request containing JSON data.\n```al\ncodeunit 50110 MyCodeunit\n{\n    procedure MakeRequest(uri: Text; payload: Text) responseText: Text\n    var\n        client: HttpClient;\n        request: HttpRequestMessage;\n        response: HttpResponseMessage;\n        contentHeaders: HttpHeaders;\n        content: HttpContent;\n        IsSuccessful: Boolean;\n    begin\n        // Add the payload to the content\n        content.WriteFrom(payload);\n\n        // Replace the default content type header with a header associated with this request\n        content.GetHeaders(contentHeaders);\n        contentHeaders.Clear();\n        contentHeaders.Add('Content-Type', 'application/json');\n\n        // Assigning content to request.Content will actually create a copy of the content and assign it.\n        // After this line, modifying the content variable or its associated headers will not reflect in\n        // the content associated with the request message\n        request.Content := content;\n\n        request.SetRequestUri(uri);\n        request.Method := 'POST';\n\n        IsSuccessful := client.Send(request, response);\n\n        if not IsSuccessful then begin\n            // handle the error\n        end;\n\n        if not response.IsSuccessStatusCode() then begin\n            // handle the error\n        end;\n\n        // Read the response content as json.\n        response.Content().ReadAs(responseText);\n    end;\n}\n```\n## Related information\nGet Started with AL<../../devenv-get-started>\nDeveloping Extensions<../../devenv-dev-overview>"}