Retrieve

Syntax

Description

Retrieves data to the DataWindow, DataWindowChild, or DataStore from the RESTFul Web service.

Applies to

RestClient object

Syntax

objectname.Retrieve ( dwControl, urlName {, data} {, tokenrequest} )

Argument

Description

objectname

A reference to the RestClient object.

dwControl

The name of the DataWindow control, DataStore, or child DataWindow.

urlName

A string whose value is the URL.

data (optional)

A string or blob data. If this argument is not specified, the retrieve function sends the request to the server with "GET" method, otherwise with "POST".

tokenrequest (optional)

A reference to the TokenRequest object for supporting oAuth 2.0.


Usage

The JSON string returned from the RESTFul Web service APIs must be in this format.

The RESTClient Retrieve function is not supported in DataWindow/DataWindowChild/DataStore with the following presentation styles: Composite, Crosstab, OLE 2.0, and RichText.

Although the RESTClient Retrieve function is not supported in the Composite DataWindow, you can call GetChild function to get the child DataWindow from the Composite DataWindow, and then call the Retrieve function to retrieve the data into the child DataWindow.

Return value

Long.

Returns values as follows. If any argument's value is null, the method returns null.

>=0 -- Returns the number of rows if it succeeds

-1 -- General error

-2 -- Invalid URL

-3 -- Cannot connect to the Internet

-4 -- Timed out

-5 -- Get token error

Example 1

This example retrieves data to a DataWindow:

long ll_return
RestClient lnv_RestClient
lnv_RestClient = Create RestClient

// Set DataObject
dw_emp.DataObject = "d_sq_gr_emp"

// Send request using GET
ll_return = lnv_RestClient.Retrieve(dw_emp, "http://demo.appeon.com/PB/webapi_client/employee/102")

// Check the return value
if ll_return >= 0 then
 MessageBox("Success", "Rows = " + String(ll_return))
else
 MessageBox("Error", "Failed to retrieve data.")
end if

Example 2

This example retrieves data to a DataStore:

long ll_return
RestClient lnv_RestClient
datastore lds_datastore

lnv_RestClient = Create RestClient
lds_datastore = create datastore

// Set DataObject
lds_datastore.DataObject = "d_sq_gr_emp"

// Send request using GET
ll_return = lnv_RestClient.Retrieve(lds_datastore, "http://demo.appeon.com/PB/webapi_client/employee/102")

// Check the return value
if ll_return >= 0 then
 MessageBox("Success", "Rows = " + String(ll_return))
else
 MessageBox("Error", "Failed to retrieve data.")
end if

Example 3

This example retrieves data to a DataWindowChild:

int li_return
RestClient lnv_restClient
DataWindowChild ldwc_dept

lnv_restClient = create RestClient

//get the DataWindowChild
dw_emp.getchild("dept_id", ldwc_dept)

//Get data from web api using GET method
li_return = lnv_restClient.retrieve(ldwc_dept, "https://192.0.3.177/appeon/api/v1/dept/list")

if li_return >= 0 then
	messagebox("Success", "Rows =  " + string(li_return))
else
	messagebox("Error", "Failed to retrieve data.")
end if

Example 4

This example passes the string data using POST method and retrieves data to a DataWindow.

long ll_return
RestClient lnv_RestClient
lnv_RestClient = Create RestClient

String ls_json = '{"empId":100, "fname":" John", "lname": "Guevara"}'

// Construct a POST request (supports all headers)
lnv_RestClient.SetRequestHeader("Content-Type", "application/json;charset=UTF-8")

// Send the POST request (add data to the body and automatically set Content-Length header)
ll_return = lnv_RestClient.Retrieve(dw_emp, "http://demo.appeon.com/PB/webapi_client/employee", ls_Json)

// Check the return value
if ll_return >= 0 then
	MessageBox("Success", "Rows = " + String(ll_return))
else
	MessageBox("Error", "Failed to retrieve data.")
end if

Example 5

This example passes the blob data using POST method and retrieves data to a DataWindow.

long ll_return
blob lblb_data
RestClient lnv_RestClient
lnv_RestClient = Create RestClient

// Construct a POST request (supports all headers)
lnv_RestClient.SetRequestHeader("Content-Type", "application/json;charset=UTF-8")
// Content-Length is set by Retrieve automatically

lblb_data = blob('{"empId":100, "fname":" John", "lname": "Guevara"}')

// Send the POST request (add data to the body and automatically set Content-Length header)
ll_return = lnv_RestClient.Retrieve(dw_emp, "http://demo.appeon.com/PB/webapi_client/employee", lblb_data)

// Check the return value
if ll_return >= 0 then
	MessageBox("Success", "Rows = " + String(ll_return))
else
	MessageBox("Error", "Failed to retrieve data.")
end if

Example 6

This example passes the string data using POST method and retrieves data to a DataStore.

long ll_return
datastore lds_datastore
RestClient lnv_RestClient

lnv_RestClient = Create RestClient
lds_datastore = create datastore
lds_datastore.dataobject = "d_sq_gr_emp"

String ls_json = '{"empId":102, "fname":" John", "lname": "Guevara"}'

// Construct a POST request (supports all headers)
lnv_RestClient.SetRequestHeader("Content-Type", "application/json;charset=UTF-8")
// Content-Length is set by Retrieve automatically

// Send the POST request (add data to the body and automatically set Content-Length header)
ll_return = lnv_RestClient.Retrieve(lds_datastore, "http://demo.appeon.com/PB/webapi_client/employee", ls_Json)

// Check the return value
if ll_return >= 0 then
	MessageBox("Success", "Rows = " + String(ll_return))
else
	MessageBox("Error", "Failed to retrieve data.")
end if

Example 7

This example passes the blob data using POST method and retrieves data to a DataStore.

long ll_return
blob lblb_data
datastore lds_datastore
RestClient lnv_RestClient

lnv_RestClient = Create RestClient
lds_datastore = create datastore
lds_datastore.dataobject = "d_sq_gr_emp"

// Construct a POST request (supports all headers)
lnv_RestClient.SetRequestHeader("Content-Type", "application/json;charset=UTF-8")
// Content-Length is set by Retrieve automatically

lblb_data =  blob('{"empId":100, "fname":" John", "lname": "Guevara"}')
// Send the POST request (add data to the body and automatically set Content-Length header)

ll_return = lnv_RestClient.Retrieve(lds_datastore, "http://demo.appeon.com/PB/webapi_client/employee", lblb_data)

// Check the return value
if ll_return >= 0 then
	MessageBox("Success", "Rows = " + String(ll_return))
else
	MessageBox("Error", "Failed to retrieve data.")
end if

Example 8

This example passes the string data using POST method and retrieves data to a DataWindowChild.

int li_return
string ls_data
RestClient lnv_restClient
DataWindowChild ldwc_dept
lnv_restClient = create RestClient

//Get DataWindowChild
dw_emp.getchild("dept_id", ldwc_dept)

ls_data  = "{'did':1}"
lnv_restClient.SetRequestHeader("Content-Type", "application/json;charset=UTF-8")
//Get data from web api using POST method

li_return = lnv_restClient.retrieve(ldwc_dept, "https://192.0.3.177/appeon/api/v1/dept/findbyid", ls_data)

if li_return >= 0 then
        messagebox("Success", "Rows =  " + string(li_return))
else
        messagebox("Error", "Failed to retrieve data.")
end if

Example 9

This example passes the blob data using POST method and retrieves data to a DataWindowChild.

int li_return
blob lblb_data
RestClient lnv_restClient
DataWindowChild ldwc_dept

lnv_restClient = create RestClient
lnv_restClient.setrequestheader("Content-Type", "Application/json;charset=utf-8")

//Convert the string to a blob
lblb_data = blob("{'did':1}", encodingutf8!)

//Get DataWindowChild
dw_emp.getchild("dept_id", ldwc_dept)

//Pass data from web api using POST method
li_return = lnv_restClient.retrieve(ldwc_dept, "https://192.0.3.177/appeon/api/v1/dept/findbyid", lblb_data)

if li_return >= 0 then
        messagebox("Success", "Rows =  " + string(li_return))
else
        messagebox("Error", "Failed to retrieve data.")
end if

Example 10

This example gets data from a Web site with token authentication and then retrieves data to a DataWindow.

integer li_return
RestClient lnv_restClient
TokenRequest lnv_tokenRequest

lnv_restClient = create RestClient
lnv_TokenRequest.tokenlocation = "http://192.0.3.177/oauth2/connect/token" //Location of the token
lnv_TokenRequest.method = "post" //Request method
lnv_TokenRequest.granttype = "client_credentials" //Grant type
lnv_TokenRequest.clientid = "Af-Bf-ZPrD0j1aXrGRsnAgx2-rcuE-ZTQOr9mEvqsi" //client ID
lnv_TokenRequest.clientsecret = "EH2OWHgxMgyKc-tN_EGZAh0Kg4-XS8AQi26vEJh" //client certificate

li_return = lnv_restClient.retrieve(dw_dept, "https://192.0.3.177/appeon/api/dept", lnv_tokenRequest)

if li_return >= 0 then
        messagebox("Success", "Rows " + string(li_return))
else
        messagebox("Error", "Failed to retrieve data.")
end if

Example 11

This example passes the blob data using POST method and retrieves the data from the Web site with token authentication to the DataWindow.

integer li_return
blob lblb_data
RestClient lnv_restClient
TokenRequest lnv_tokenRequest

lnv_restClient = create RestClient

lnv_TokenRequest.tokenlocation = "http://192.0.3.177/oauth2/connect/token" //Location of the token
lnv_TokenRequest.method = "post" //Request method
lnv_TokenRequest.granttype = "client_credentials" //Grant type
lnv_TokenRequest.clientid = "Af-Bf-ZPrD0j1aXrGRsnAgx2-rcuE-ZTQOr9mEvqsi" //client ID
lnv_TokenRequest.clientsecret = "EH2OWHgxMgyKc-tN_EGZAh0Kg4-XS8AQi26vEJh" //client certificate

lnv_restClient.setrequestheader("Content-Type", "Application/json;charset=utf-8")

lblb_data = blob("{'did':1}")

li_return = lnv_restClient.retrieve(dw_dept, "https://192.0.3.177/appeon/api/dept", lblb_data, lnv_tokenRequest)

if li_return >= 0 then
	messagebox("Success", "Rows " + string(li_return))
else
	messagebox("Error", "Failed to retrieve data.")
end if

JSON formats (required by RESTClient object)

The JSON string returned from the RESTFul Web Service APIs must have no more than 2 levels, and the top-level must be arrays, the second-level must be objects.

For example, the following JSON string is supported:

[
    {
       "empid":148,
       "fname":"julie",
       "lname":"jordan"
    },
    {
       "empId":184,
       "fname":"Melissa",
       "lname":"Espinoza"
    },
    {
       "empId":191,
       "fname":"Jeannette",
       "lname":"bertrand"
    }
]

The following JSON string is unsupported:

{
        "empId":191,
        "fname":"Jeannette",
        "lname":"bertrand",
         "manager":
                  {
                           "managerid":703,
                           "fname":"David",
                           "lname":"Scott"
                   },
         "department":
                   {
                           "deptid":200,
                           "name":"Sales"
                   }
}