HTTP Class and Functions - Business Central

 You can access REST services through HTTP requests. To implement an HTTP request, you need to use an HTTP verb.

The following verbs are the most-used verbs in REST services:

  • GET (to read data)

  • POST (to create data)

  • PUT (to update/replace data)

  • PATCH (to update/modify data)

  • DELETE (to delete data)






HttpClient class

The HttpClient class provides a base class for sending HTTP requests and receiving HTTP responses from a resource that is identified by a URI.

  • DefaultRequestHeaders() - Gets the default request headers that should be sent with each request.

  • SetBaseAddress(Uri) - Sets the base address of the URI that is used when you are sending requests.

  • Get(Uri, HttpResponseMessage) - Sends a GET request to get the resource that is identified by the request URI.

  • Post(Uri, HttpContent, HttpResponseMessage) - Sends a POST request to the specified URI.

  • Put(Uri, HttpContent, HttpResponseMessage) - Sends a PUT request to the specified URI.

  • Delete(Uri, HttpResponseMessage) - Sends a DELETE request to the specified URI.

  • Send(HttpRequestMessage, HttpResponseMessage) - Sends an HTTP request. In the HttpRequestMessage variable, you must provide the URI and the verb to use.

  • Clear() - Sets the HttpClient variable to the default value.

  • Timeout() - Gets or sets the duration in seconds to wait before the request times out. Timeout is an integer value.

  • AddCertificate(Certificate, Password) - Adds a certificate to the HttpClient class. You need to provide a base64 encoded certificate.

When providing a URI, you can use two approaches. The first approach is to request data from the following URL:

https://jsonplaceholder.typicode.com/posts

Then, you can use the SetBaseAddress when you are using the HttpClient class to perform multiple requests.

var

    client: HttpClient;

    responseMessage: HttpResponseMessage;

begin 

    client.SetBaseAddress('https://jsonplaceholder.typicode.com/');

    client.Get('posts', responseMessage);

end;

The second approach is for when you are only requesting the data once. In that case, you can put the full URI in the Get function.

var

    client: HttpClient;

    responseMessage: HttpResponseMessage;

begin 

    client.Get('https://jsonplaceholder.typicode.com/posts', responseMessage);

end;

HttpHeaders class

The HttpHeaders class contains a collection of headers and their values. HttpHeaders are sent with every request and response, and they are used to send extra information about the request or how the client wants the response to be formatted.

Common HttpHeaders are Authorization (used to send authentication credentials), Content-Type (the media type of the body of the request), User-Agent (the user agent string, the name of the browser), Accept-Charset (which character sets are acceptable).

You can get the HttpHeaders by using the DefaultRequestHeaders property from the HttpClient class.

var

    client: HttpClient;

    headers: HttpHeaders;    

    content: HttpContent;

    responseMessage: HttpResponseMessage;

begin 

    headers := client.DefaultRequestHeaders();

    headers.Add('Content-Type','application-json');

    client.Post('https://jsonplaceholder.typicode.com/posts', content, 

                responseMessage);

end;

  • Add(Key, Value) - Sets the provided value for the provided header name.

  • Clear() - Sets the HttpHeaders variable to the default value.

  • Contains(Key) - Checks if an HttpHeaders variable contains a property with the given key.

  • GetValues(Key, Array of Text) - Get the values for the specified key. The values are returned in the array of text.

  • Remove(Key) - Removes the key and the related values from the HttpHeaders object.

HttpResponseMessage class

The HttpResponseMessage class represents an HTTP response message. A response message is the result of an HTTP action (GetPostPutDelete). It is returned in the HttpResponseMessage parameter.

var

    client: HttpClient;

    responseMessage: HttpResponseMessage;

begin 

    client.Get('https://jsonplaceholder.typicode.com/posts', responseMessage);

end;

  • Content() - Gets the contents (HttpContent) of the HTTP response.

  • Headers() - Gets the HTTP request's HTTP headers.

  • HttpStatusCode() - Gets the status code of the HTTP response.

  • IsSuccessStatusCode() - Gets a value that indicates if the HTTP response was successful.

  • ReasonPhrase() - Gets the reason phrase that is typically sent together with the status code. You can use this variable to give the user extra information when a request failed.

HttpContent class

The HttpContent class represents an HTTP body and content headers, and it is used as the body to send information or as a response body. You can use the Content property on the HttpRequestMessage (with a request) or the HttpResponseMessage (with a response) classes.

  • Clear() - Sets the HttpContent object to a default value.

  • GetHeaders(HttpHeaders) - Gets the content's HTTP headers. The result is returned in the HttpHeaders parameter.

  • ReadAs(Result) - Reads the content into the provided text or stream. The result can be of type Text or InStream.

  • WriteFrom(Value) - Sets the HttpContent variable to the provided text or stream.

HttpRequestMessage class

The HttpRequestMessage class represents an HTTP request message. A request message is the class that is used to send a request.

[Ok := ] HttpClient.Send(HttpRequestMessage, HttpResponseMessage);

  • Content() - Gets/sets the contents of the HTTP request.

  • GetRequestUri() - Gets the URI that is used for the HTTP request.

  • Method() - Gets or sets the method type. You must provide an HTTP verb that is used with this request.

  • SetRequestUri(RequestUri) - Sets the URI that is used for the HTTP request.

var
    client: HttpClient;
    requestMessage: HttpRequestMessage;
    responseMessage: HttpResponseMessage;
begin 
    requestMessage.Method('GET');
    requestMessage.SetRequestUri('https://jsonplaceholder.typicode.com/posts');
    client.Send(requestMessage, responseMessage);
end;


No comments:

Post a Comment