This document explains how to insert new data using a Business Component exposed as a Rest web service in GeneXus.
First, let's see the basic guidelines:
Use the HttpClient data type.
The HTTP method used for inserting new data is the HTTP POST method.
All the parameters for inserting new data using the BC (except the PK parameters) need to be included in the body of the HTTP request. The message body should be in JSON format and contain the structure of the Business Component as it comes when it makes an HTTP GET.
The easiest way to build the body with the correct format is by defining an SDT based on the BC structure and using the ToJson method to format it to a valid JSON string.
That JSON string needs to be added to the body of the HTTP request before executing the POST to the service.
<server uri>/rest/<module>/<bc name>/<param1>,<param2>,..,<paramN>
Where <param1>,<param2>,..,<paramN> is the compound Primary Key.
If the Primary Key is autonumbered, just use 0 in the URL.
Consider the Product Business Component transaction that is exposed as a Rest web service, as shown in the figure below:
In this example, we want to insert a new Product, whose ProductId is 5.
We've defined a ProductSDT structured data type based on the Product structure, as shown in the figure below:
Then, the code would be as follows:
&httpclient.Port = &port
&httpclient.BaseUrl = &urlbase
&productSDT.ProductName = "Samsung Galaxy"
&productSDT.ProductPrice = 22
if &httpclient.StatusCode = 201
msg("Data successfully added")
msg("There was an error retrieving the data: " + &httpclient.StatusCode.ToString())
Download the sample from Sample insert data using a Rest BC
- Do not forget to add the 'content-type:application/json' header.
- If more than one parameter should be passed in the URL (a compound PK), they need to be separated by commas.
- If the service URI is "http://localhost/TestRESTFullGX.NetEnvironment/rest/Product", the BASE URL is: "/TestRESTFullGX.NetEnvironment/rest/"
- Error handling is managed by querying the HTTP Status Code after the invocation.
Business Components as Rest web services in GeneXus