Quick start to using SQL Anywhere as a web client

This topic illustrates how to use SQL Anywhere as a web client application to connect to a SQL Anywhere HTTP server and access a general HTTP web service. It does not illustrate SQL Anywhere web client capabilities to a full extent. Many SQL Anywhere web client features are available that are beyond the scope of this topic.

The following tasks are performed:

  • Create and start a SQL Anywhere web client.

  • Create a procedure that connects to a web service on an HTTP server.

  • Perform operations on the result set sent by the HTTP server.

You can develop SQL Anywhere web client applications that connect to any type of online web server, but this guide assumes that you have started a local SQL Anywhere HTTP server on port 8082 and want to connect to a web service named SampleHTMLService, created with the following SQL statements:



CREATE SERVICE SampleHTMLService
    TYPE 'HTML'
    USER DBA
    AUTHORIZATION OFF
    AS CALL sp_echo(:i, :f, :s);

CREATE PROCEDURE sp_echo(i INTEGER, f REAL, s LONG VARCHAR)
RESULT(ret_i INTEGER, ret_f REAL, ret_s LONG VARCHAR)
BEGIN
    SELECT i, f, s;
END;

For more information about setting up an HTTP server, see Quick start to using SQL Anywhere as an HTTP web server.

 Create a SQL Anywhere web client application
  1. Run the following command to create a SQL Anywhere client database if one does not already exist:

    dbinit client-database-name

    Replace client-database-name with a new name for your client database.

  2. Run the following command to start the client database:

    dbsrv12 client-database-name.db
  3. Run the following command to connect to the client database through Interactive SQL:

    dbisql -c "UID=DBA;PWD=sql;SERVER=client-database-name"
  4. Create a new client procedure that connects to the SampleHTMLService web service using the following SQL statement:

    CREATE PROCEDURE client_post(f REAL, i INTEGER, s VARCHAR(16), x VARCHAR(16))
        URL 'http://localhost:8082/SampleHTMLService'
        TYPE 'HTTP:POST'
        HEADER 'User-Agent:SATest';
  5. Execute the following SQL statement to call the client procedure and send an HTTP request to the web server:

    CALL client_post(3.14, 9, 's varchar', 'x varchar');

The HTTP POST request created by client_post looks something like this.



POST /SampleHTMLService HTTP/1.0
ASA-Id: ea1746b01cd0472eb4f0729948db60a2
User-Agent: SATest
Accept-Charset: windows-1252, UTF-8, *
Date: Wed, 9 Jun 2010 21:55:01 GMT
Host: localhost:8082
Connection: close
Content-Type: application/x-www-form-urlencoded; charset=windows-1252
Content-Length: 58

&f=3.1400001049041748&i=9&s=s%20varchar&x=x%20varchar

The web service SampleHTMLService running on the web server extracts the parameter values for i, f, and s from the POST request and passes them as parameters to the sp_echo procedure. Parameter value x is ignored. The sp_echo procedure creates a result set which is returned to the web service. It is important to note that agreement in parameter names between the client and the web server is essential for proper mapping.

The web service creates the response which is sent back to the client. The output displayed in Interactive SQL should be similar to the following output:

Attribute Value
Status HTTP/1.1 200 OK
Body


<html>
<head>
<title>/SampleHTMLService</title></head>
<body>
<h3>/SampleHTMLService</h3>
<table border=1>
<tr class="header"><th><b>ret_i</b></th>
<th><b>ret_f</b></th>
<th><b>ret_s</b></th>
</tr>
<tr><td>9</td><td>3.1400001049041748</td><td>s varchar</td>
Date Wed, 09 Jun 2010 21:55:01 GMT
Connection close
Expires Wed, 09 Jun 2010 21:55:01 GMT
Content-Type text/html; charset=windows-1252
Server SQLAnywhere/12.0.1
 See also