Setting up a REST connection

I am converting an existing VCL application into a Web application, which is progressing very well. I am now trying to connect to a database on a server. My customer only provides me with a simple php access to the database, using two parameters: an SQL statement and a database type (live and test). The database user is a read-only one. So, I can only read data from it.

To use this database, I am using the REST modules from Delphi, providing a REST Client, a REST response and a REST request, as shown below. I am trying to use the same principle for the web application, but could only find a REST client object. I also read the documentation about an XData connection, but got lost, as I didn't see how to connect to this php service. I also couldn't find demos to help me out and there are support REST requests for Web Core application.

Would it be possible to give me some pointers about how to start this REST connection? The code below shows two simple methods in VCL:

  • One to set up the REST objects.
  • One to make a call to the database and get a JSON string.

I assume the solution in WEB Core is equally simple, but I don't see the starting point. E.g. a property of a vcl.RESTClient is "Accept", which does not exist in WebLib.RestClient.

Kind regards,

Michel Huybrechts
Micriconsult BV

procedure CTRESTCommSqlEngine.SetupREST(const UserName: UnicodeString;
const Password: UnicodeString;
const BaseURL: UnicodeString);
{
Abstract:
Sets up a rest connection to the server.
}
const
REST_ACCEPT = 'application/json, text/plain; q=0.9, text/html;q=0.8,';
REST_CHARSET = 'utf-8, *;q=0.8';
var
BasicAuthenticator: THTTPBasicAuthenticator;
begin { CTRESTCommSqlEngine.SetupREST }
if v_RESTClient = nil
then
begin
BasicAuthenticator := THTTPBasicAuthenticator.Create(UserName, Password);
v_RESTClient := TRESTClient.Create(v_AOwner);
v_RESTClient.Name := 'Rest_Client';
v_RESTClient.Accept := REST_ACCEPT;
v_RESTClient.AcceptCharSet := REST_CHARSET;
v_RESTClient.RaiseExceptionOn500 := False;
v_RESTClient.BaseURL := BaseURL;
v_RESTClient.Authenticator := BasicAuthenticator;
v_RESTResponse := TRESTResponse.Create(v_AOwner);
v_RESTResponse.Name := 'Rest_Response';
v_RESTRequest := TRESTRequest.Create(v_AOwner);
v_RESTRequest.Name := 'Rest_Request';
v_RESTRequest.Accept := REST_ACCEPT;
v_RESTRequest.AcceptCharSet := REST_CHARSET;
v_RESTRequest.Method := rmPOST { All requests here are POST };
v_RESTRequest.SynchronizedEvents := False;
v_RESTRequest.Client := v_RESTClient;
v_RESTRequest.Response := v_RESTResponse
end
end { CTRESTCommSqlEngine.SetupREST };

function CTRESTCommSqlEngine.SendSQLStmt(const Resource: UnicodeString;
const SQLStmt: UnicodeString): TXMLDocument;
{
Abstract:
Sends a SQL select statement and create objects from the JSON string
}
var
JSONStr: UnicodeString;
begin { CTRESTCommSqlEngine.SendSQLStmt }
v_RESTRequest.Resource := Resource { Name of the php service };
v_RESTRequest.AddParameter(PARAM_SQL, SQLStmt, { PARAM_SQL = 'sql' }
TRESTRequestParameterKind.pkGETorPOST);
v_RESTRequest.AddParameter(PARAM_DB, ACCESS_DB, { PARAM_DB = 'db', ACCESS_DB='live' }
TRESTRequestParameterKind.pkGETorPOST);
v_RESTResponse.ContentType := 'application/json';
v_RESTRequest.Execute;
v_RESTRequest.Params.Delete(PARAM_SQL);
v_RESTRequest.Params.Delete(PARAM_DB);
JSONStr := v_RESTResponse.Content;
Result := JSSONToObject(JSONStr)
end { CTRESTCommSqlEngine.SendSQLStmt };

Since I see that you use basic authentication and your PHP returns JSON, I would expect that you can directly use TWebClientConnection.
You set the username/password base64 encoded in the WebClientConnection.Headers and can perform the request to the HTTP URL. If this returns the data as JSON array, it will be automatically fetched via the TWebClientDataSet using the TWebClientConnection.

Dear Sir,

Thank you for your answer. I will check and try all your suggestions. Looks like they will solve my problem. I guess I will need a bit of time to get acquainted with these objects.

Kind regards,

Michel Huybrechts

Micriconsult BV

www.micriconsult.be