Official Content

This document explains how to retrieve data from a Business Component exposed as a Rest web services in GeneXus.

First, see the basic guidelines:

1. How are the Rest services called in GeneXus?

Use the HttpClient data type.

2. Which HTTP method should be used to retrieve the data?

The HTTP method used for retrieving the data is the GET HTTP method.

3. What kind of information is exposed by the web service, so it can be consumed?

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.

4. Which is the URL format to execute the GET?

<server uri>/rest/<module>/<bc name>/<param1>,<param2>,..,<paramN>

Where: 

<param1>,<param2>,..,<paramN>
      Is the compound Primary Key.

Samples

The following figure shows the Invoice Business Component transaction that is exposed as a Rest web service:

i2016_03_29_20_21_191_png

In this example, we want to get the Invoice whose InvoiceId=5

Sample code

&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."}}.

See Also

Business Components as Rest web services in GeneXus

Last update: November 2024 | © GeneXus. All rights reserved. GeneXus Powered by Globant