The following example shows a REST GeneXus procedure that returns information about a Customer, given its Identification.
The GeneXus procedure (called "GetCustomer") is declared as REST by setting the following properties:
- Expose as web service = True
- Rest Protocol = True
The code is very simple, as shown below:
where CustomerId = &CustomerId
&customerName = CustomerName
&CustomerBirthDate = CustomerBirthDate
&CustomerPayDate = CustomerPayDate
The GeneXus client can be as follows:
&httpclient.Host =&host // &httpclient is an HTTPClient variable data type
&httpclient.Port = &port
&httpclient.BaseUrl = &baseurl //Example: /webappname/rest/
&body = &customersdt.ToJson()
&lvc = &httpclient.ToString()
Since the response is in Json format:
the result should be processed using , loading the Json response in an SDT (Structure SDT Object) with the corresponding structure.
- The Json code (in the example, the variable &body) must include only the items declared as parameters in the parm rule.
- The values in the Json code have to be CamelCase as defined in the parm rule of the REST procedure.
In general, set the following:
||Content-Type: application/json; charset=UTF-8
Based on the example that calls a 'GetCustomer' object, a sample request is as follows:
Content-Type: application/json; charset=UTF-8
User-Agent: Apache-HttpClient/4.3.1 (java 1.5)
And the response is as follows:
HTTP/1.1 200 OK
Last-Modified: Wed, 23 Jul 2014 06:29:04 GMT
Date: Wed, 23 Jul 2014 06:29:05 GMT
Rest web services in GeneXus