HowTo: Consuming a Procedure exposed as a Rest service

Unofficial Content

The following is a simple example where we consume a procedure exposed as a Rest web service, which only receives simple parameters and doesn't return any.

In the example, the "AddCustomer" Procedure is declared as REST web service in GeneXus, so the following properties are set as described here:

  • Expose as web service = true
  • Rest protocol = true

exposeaswsprop2

The procedure receives "in" parameters in order to add a Customer to the corresponding table:

Parm Rule:

parm(in:&CustomerId,in:&CustomerName,in:&CustomerBirthDate);

Source:

new
   CustomerId = &CustomerId
   CustomerName = &CustomerName
   CustomerBirthDate = &CustomerBirthDate
endnew

In order to call AddCustomer as a REST web service, you should use HttpClient data type. A GeneXus client should look as shown below:

&httpclient.Host =&host
&httpclient.Port = &port
&httpclient.BaseUrl = &baseurl
 
&body = &customersdt.ToJson() //&customersdt is a variable of CustomerSdt data type which is an SDT that includes the CustomerId, CustomerName and CustomerBirthDate items.
 
&httpclient.AddHeader('Content-type','application/json')
&httpclient.AddString(&body)

&httpclient.Execute('POST','AddCustomer') 
//Then process the HttpClient response.

Download the sample from Sample consuming a Rest procedure

As shown in the example, the parameters are sent in Json format in the body of the HttpRequest; that's why in the example we use the method to convert the data contained in an SDT to Json format.

 

Note:
With respect to consuming a Rest service (it can be generated by GeneXus or not), GeneXus provides the OpenAPI import tool.

 

Next, read Procedures as REST: Using SDT as input to the procedure

See Also

Rest web services in GeneXus