Warning: This document describes how to upload a blob to a REST Service, defined as a procedure in another KB. To upload a blob (or image, video or audio) using a procedure in the same KB, just use the Call command passing the attribute or variable as a parameter..
This is the same example as Procedures as REST: Using SDT as input to the procedure. In addition, it shows how to send a blob variable data type as input to the Rest web service procedure.
The example consists of adding a customer to the Customer table, where there is a blob field representing the customer's photo.
The "AddCustomer" procedure is the same as the one in the example Procedures as REST: Using SDT as input to the procedure.
The client consumer's particularity is that it has to do two HTTP POSTs:
1. An HTTP POST to "gxobject" (under the web application URL) attaching the file (blob) to the HTTP Request. This returns a reference which should be used to insert the blob in the database table.
2. Next, it executes an HTTP POST to the REST procedure sending a Json request which includes the information to be processed.
Source Code of the GeneXus Client:
&ImagePath = 'CustomerPhoto.jpg'
&httpclient.Host = &host //&httpclient is an HTTPClient variable data type
&httpclient.Port = &port
&httpclient.BaseUrl = &urlbase //example:'/webappname/'
&httpclient.AddHeader('Content-type',' application/jpg ')
&httpclient.AddFile(&ImagePath)
&httpclient.Execute('POST','gxobject')
&lvc = &httpclient.ToString()
&blobref.FromJson(&lvc)
&ref = &blobref.object_id //&blobref is based on blobref data type (*)
&Cache = Cache.getCache("FL") //this line only applies to V15 or higher version
&ref = "gxupload:" + &cache.Get(&ref.Substring(&ImagePath.IndexOf(!':')+1)) //this line only applies to V15 or higher version
&customersdt.CustomerId= &customerId
&customersdt.CustomerName = &customerName
&customersdt.Customerbirthdate = &customerbirthdate
&customersdt.CustomerPayDate = &customerPayDate
&customersdt.CustomerPhoto = &ref
&body = '{"Customersdt":' + &customersdt.ToJson() + '}'
//The following POST is to another BaseURL
&httpclient.BaseUrl = &urlbase + '/rest/' //example: '/webappname/rest/'
&httpclient.AddHeader('Content-type','application/json')
&httpclient.AddString(&body)
&httpclient.Execute('POST','AddCustomer')
(*) blobref data type:

Lastly, it processes the HTTP Client response.
Note: Regarding on how to consume a REST service (generated by GeneXus or not), GeneXus provides the OpenAPI import tool.
Rest web services in GeneXus
|