Procedures as REST: Using SDT as input to the procedure

Unofficial Content

In a real-life situation, you will probably need to provide feedback about the operation to the user, so you can use a Business Component to perform actions in the database tables and return the operation results to the caller consumer.

In the example, "AddCustomer" Procedure is declared as REST web service in GeneXus.

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

exposeaswsprop2

It receives an SDT as parameter, containing the information of the customer to be added to the Customer table.
Also, it returns the error and warning messages in the variable &messages, in order to be able to provide feedback about the operation to the user.

IMPORTANT NOTE: Rest procedures can receive any type of parameters: simple or SDTs, collections or not.

Parm Rule:

parm(in:&Customersdt,out:&messages);

Source:

&Customer.Load(&Customersdt.CustomerId)

if &Customer.Fail()
 &Customer = new()
 &Customer.CustomerId = &Customersdt.CustomerId
endif

&Customer.CustomerName = &Customersdt.CustomerName
&Customer.CustomerBirthDate = &Customersdt.CustomerBirthDate
&Customer.CustomerPayDate = &Customersdt.CustomerPayDate
&customer.CustomerPhoto = &customersdt.CustomerPhoto

&customer.Save()
&messages = &customer.GetMessages()
commit


Variable &Customersdt is based on Customersdt Structured Data Type:

customersdtstructure

A GeneXus client for this REST procedure would be as follows:

&httpclient.Host = &host
&httpclient.Port = &port
&httpclient.BaseUrl = &baseurl

&body = '{"Customersdt":' + &customersdt.ToJson() + '}'
 
&httpclient.AddHeader('Content-type','application/json')
&httpclient.AddString(&body)

&httpclient.Execute('POST','AddCustomer')

Then process the HTTP Client response.

NOTE:

1. The BaseURL for calling REST procedures is <webappname>/rest.
2. Note that the HTTP request is in Json format. Since the procedure receives an SDT (Structured Data Type), the Json expected is preceded by the name of the SDT, which has to be in the same casing as the parameter of the procedure (&Customersdt).
So, the HTTP Request is as follows:

{"Customersdt":{"CustomerId":129,"CustomerName":"Rodolfo","CustomerBirthDate":"1976-08-05","CustomerPayDate":"1976-08-05T00:31:00","CustomerPhoto":"12356"}}

See Also

Rest web services in GeneXus