Get and Post Method in Business Central - CloudFronts

Get and Post Method in Business Central

Introduction:

HTTP (Hypertext Transfer Protocol) is a protocol used for communication between web servers and clients. When a client sends a request to a server, it is called an HTTP request. The request consists of a request line, headers, and an optional message body. The request line contains the HTTP method, URL, and HTTP version. The headers contain additional information about the request, such as the user agent and content type. 

When the server receives the request, it sends back an HTTP response. The response consists of a status line, headers, and an optional message body. The status line contains the HTTP version, status code, and reason phrase. The headers contain additional information about the response, such as the content type and server type.

Pre-requisites:

  1. Business Central OnCloud or OnPremise

Configuration:

So, in this we are going to use table extension of the table Item [Record id: 27].

It extends the “Item List” page by adding a button after the “History” action. The button is defined as an action with the following properties: ApplicationArea, Visible, Image, and trigger OnAction(). When the button is triggered, it sends an HTTP request using the HTTPRequestFact() and HTTPRequestFactPost() functions.

  1. GET Request():

The HttpClient.Get method to send an HTTP GET request to retrieve data from a remote server. The syntax for the HttpClient.Get method is as follows:

[Ok := ] HttpClient.Get (Path: Text, var Response: HttpResponseMessage)

Here, Path is the path of the resource you want to retrieve, and Response is the response received from the remote endpoint. The HttpClient.Get method returns a Boolean value indicating whether the operation was successful or not. If the operation was successful, the Response parameter will contain the response received from the remote endpoint.

This code sends an HTTP GET request to the URL https://ene6g2z8lzf2g.x.pipedream.net/ using the HttpClient.Get method. The response received from the remote endpoint is stored in the Httpresponsemessage variable. If the operation was successful, the response is read as a string and stored in the Response variable. Finally, the Message function is called to display the response on the screen.

  1. POST Request():

The HttpClient.Post method to send an HTTP POST request to a remote server. The syntax for the HttpClient.Post method is as follows:

[Ok := ] HttpClient.Post (Path: Text, Content: HttpContent, var Response: HttpResponseMessage)

Here, Path is the path of the resource you want to post data to, Content is the HTTP request content sent to the server, and Response is the response received from the remote endpoint. The HttpClient.Post method returns a Boolean value indicating whether the operation was successful or not. If the operation was successful, the Response parameter will contain the response received from the remote endpoint.

This code sends an HTTP POST request to the URL https://enni1en7jg0n.x.pipedream.net/ with the JSON payload constructed from the records in the current page. The HttpClient.Post method is used to send the HTTP POST request. The response received from the remote endpoint is stored in the Httpresponsemessage variable. If the operation was successful, the response is read as a string and stored in the Response variable. Finally, the Message function is called to display the response on the screen.

The code first sets the filter for the current page to the “Item” record. It then loops through all the records in the “Item” record and creates a new JSON object for each record. The JSON objects are added to a JSON array. A new JSON object is created for the JSON array, and the JSON array is added to the JSON object. The JSON object is then written to a text variable.

A new HTTP content object is created for the JSON payload, and the JSON payload is written to the HTTP content object. The HTTP POST request is then sent to the remote endpoint using the HttpClient.Post method. If the operation was successful, the response is read as a string and stored in the Response variable. Finally, the Message function is called to display the response on the screen.

  1. Basic Authentication

The RequestWithBasicAuthenticaton procedure takes in three parameters: Url, Username, and Password. It creates an HTTP request message with the given URL and sets the method to GET. It then adds the authorization method by encoding the username and password in Base 64 and adding it to the request headers. After that in if condition it sends an HTTP GET request to the URL specified in the Url parameter with basic authentication using the HttpClient.Send method. The HttpClient.Send method is used to send the HTTP GET request. The response received from the remote endpoint is stored in the ResponseMessage variable. If the operation was successful, the response is read as a string and stored in the Response variable. Finally, the Message function is called to display the response on the screen.

Output:

For Multiple Data,

Conclusion:

In conclusion, this blog has provided valuable insights into the fundamental concepts of HTTP GET and POST methods, along with an exploration of Basic Authentication in the discussed code. Readers now have a clearer understanding of how these key HTTP techniques, combined with authorization mechanisms, play pivotal roles in web communication, facilitating data retrieval, creation, and ensuring secure access.

Introduction:

HTTP (Hypertext Transfer Protocol) is a protocol used for communication between web servers and clients. When a client sends a request to a server, it is called an HTTP request. The request consists of a request line, headers, and an optional message body. The request line contains the HTTP method, URL, and HTTP version. The headers contain additional information about the request, such as the user agent and content type. 

When the server receives the request, it sends back an HTTP response. The response consists of a status line, headers, and an optional message body. The status line contains the HTTP version, status code, and reason phrase. The headers contain additional information about the response, such as the content type and server type.

Pre-requisites:

  1. Business Central OnCloud or OnPremise

Configuration:

So, in this we are going to use table extension of the table Item [Record id: 27].

It extends the “Item List” page by adding a button after the “History” action. The button is defined as an action with the following properties: ApplicationArea, Visible, Image, and trigger OnAction(). When the button is triggered, it sends an HTTP request using the HTTPRequestFact() and HTTPRequestFactPost() functions.

  1. GET Request():

The HttpClient.Get method to send an HTTP GET request to retrieve data from a remote server. The syntax for the HttpClient.Get method is as follows:

[Ok := ] HttpClient.Get (Path: Text, var Response: HttpResponseMessage)

Here, Path is the path of the resource you want to retrieve, and Response is the response received from the remote endpoint. The HttpClient.Get method returns a Boolean value indicating whether the operation was successful or not. If the operation was successful, the Response parameter will contain the response received from the remote endpoint.

This code sends an HTTP GET request to the URL https://ene6g2z8lzf2g.x.pipedream.net/ using the HttpClient.Get method. The response received from the remote endpoint is stored in the Httpresponsemessage variable. If the operation was successful, the response is read as a string and stored in the Response variable. Finally, the Message function is called to display the response on the screen.

  1. POST Request():

The HttpClient.Post method to send an HTTP POST request to a remote server. The syntax for the HttpClient.Post method is as follows:

[Ok := ] HttpClient.Post (Path: Text, Content: HttpContent, var Response: HttpResponseMessage)

Here, Path is the path of the resource you want to post data to, Content is the HTTP request content sent to the server, and Response is the response received from the remote endpoint. The HttpClient.Post method returns a Boolean value indicating whether the operation was successful or not. If the operation was successful, the Response parameter will contain the response received from the remote endpoint.

This code sends an HTTP POST request to the URL https://enni1en7jg0n.x.pipedream.net/ with the JSON payload constructed from the records in the current page. The HttpClient.Post method is used to send the HTTP POST request. The response received from the remote endpoint is stored in the Httpresponsemessage variable. If the operation was successful, the response is read as a string and stored in the Response variable. Finally, the Message function is called to display the response on the screen.

The code first sets the filter for the current page to the “Item” record. It then loops through all the records in the “Item” record and creates a new JSON object for each record. The JSON objects are added to a JSON array. A new JSON object is created for the JSON array, and the JSON array is added to the JSON object. The JSON object is then written to a text variable.

A new HTTP content object is created for the JSON payload, and the JSON payload is written to the HTTP content object. The HTTP POST request is then sent to the remote endpoint using the HttpClient.Post method. If the operation was successful, the response is read as a string and stored in the Response variable. Finally, the Message function is called to display the response on the screen.

  1. Basic Authentication

The RequestWithBasicAuthenticaton procedure takes in three parameters: Url, Username, and Password. It creates an HTTP request message with the given URL and sets the method to GET. It then adds the authorization method by encoding the username and password in Base 64 and adding it to the request headers. After that in if condition it sends an HTTP GET request to the URL specified in the Url parameter with basic authentication using the HttpClient.Send method. The HttpClient.Send method is used to send the HTTP GET request. The response received from the remote endpoint is stored in the ResponseMessage variable. If the operation was successful, the response is read as a string and stored in the Response variable. Finally, the Message function is called to display the response on the screen.

Output:

For Multiple Data,

Conclusion:

In conclusion, this blog has provided valuable insights into the fundamental concepts of HTTP GET and POST methods, along with an exploration of Basic Authentication in the discussed code. Readers now have a clearer understanding of how these key HTTP techniques, combined with authorization mechanisms, play pivotal roles in web communication, facilitating data retrieval, creation, and ensuring secure access.


Share Story :

Secured By miniOrange