HttpContent Data Type
Version: Available or changed with runtime version 1.0.
Represents an HTTP entity body and content headers.
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. |
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(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.
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;
begin
// Add the payload to the content
content.WriteFrom(payload);
// Retrieve the contentHeaders associated with the content
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';
client.Send(request, response);
// Read the response content as json.
response.Content().ReadAs(responseText);
end;
}
See Also
Feedback
Submit and view feedback for