This document explains how to retrieve data from a Business Component exposed as a Rest web services in GeneXus.
First, see the basic guidelines:
Use the HttpClient data type.
The HTTP method used for retrieving the data is the GET HTTP method.
You can get a record by giving its Primary Key.
However, since GeneXus X Evolution 2 you cannot retrieve all the data of a table (you can't execute a GET of all the records, e.g: <url base>/rest/Products), nor get the data from a given Foreign Key (<url base>/rest/Invoices?ClientId=234), or the data for a given Description Attribute (e.g:<url base>/rest/Products?ProductName=SmartPhone). This restriction on the information provided by the Rest BC is due to security reasons.
<server uri>/rest/<module>/<bc name>/<param1>,<param2>,..,<paramN>
Where:
<param1>,<param2>,..,<paramN>
Is the compound Primary Key.
The following figure shows the Invoice Business Component transaction that is exposed as a Rest web service:
In this example, we want to get the Invoice whose InvoiceId=5
&httpclient.Host= &server
&httpclient.Port = &port
&httpclient.BaseUrl = &urlbase
&httpclient.Execute('GET','Invoice/5')
if &httpclient.StatusCode = 200
&result = &httpclient.ToString()
else
msg("There was an error retrieving the data: " + &httpclient.StatusCode.ToString())
endif
Download the sample from Sample GET data using a Rest BC
Notes:
- If more than one parameter should be passed in the URL, they need to be separated by commas.
- If the service URI is http://localhost/TestRESTFullGX.NetEnvironment/rest/Invoice, the BASE URL is: /TestRESTFullGX.NetEnvironment/rest/
- Error handling is managed by querying the HTTP Status Code after the invocation. For example, this error can occur if the data doesn't exist: {"error":{"code":"404","message":"Data with the specified key could not be found."}}.
Business Components as Rest web services in GeneXus