Official Content

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 "<Object name>/gxobject" (under the web application URL) attaching the file (blob) to the HTTP Request. This returns a reference that 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.

POST /gxobject
Host: example.com
Content-Length: 808
Content-Type: image/jpg
[file content goes there]. -> This must be Format = Binary

Source Code of the GeneXus Client:

&ImagePath = MySampleImage.Link()
&httpclient.Host = &host //&httpclient is an HTTPClient variable data type
&httpclient.Port = &port
&httpclient.BaseUrl = &urlbase + '/rest/' //example: '/webappname/rest/'  

&httpclient.AddHeader(!'Content-type', !'application/jpg')
&httpclient.AddFile(&ImagePath)
&httpclient.Execute(!'POST', !'AddCustomer/gxobject')
 
&GXObjectUploadResponse.FromJson(&httpclient.ToString()) //&GXObjectUploadResponseis based on GXObjectUploadResponse data type defined below (*)

&customersdt.CustomerId= &customerId
&customersdt.CustomerName = &customerName
&customersdt.Customerbirthdate = &customerbirthdate
&customersdt.CustomerPayDate = &customerPayDate
&customersdt.CustomerPhoto = &GXObjectUploadResponse.object_id 

&body = Format(!'{"Customersdt":%1 }', &customersdt.ToJson())

&httpclient.AddHeader(!'Content-type', !'application/json')
&httpclient.AddString(&body)

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

(*) GXObjectUploadResponse data type:

image_20221012113536_1_png

Lastly, it processes the HTTP Client response.

Note: GeneXus provides the OpenAPI import tool for consuming a Rest service, whether it's been generated by GeneXus or not.

Download a Sample here:

GXObjectUploadTransaction

See Also

Rest web services in GeneXus

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