Click here to view and discuss this page in DocCommentXchange. In the future, you will be sent there automatically.

SAP Sybase SQL Anywhere 16.0 » SQL Anywhere Server - Programming » HTTP web services » HTTP web service examples » Tutorial: Create a web server and access it from a web client

 

Lesson 1: Setting up a web server to receive requests and send responses

The goal of this lesson is to set up a SQL Anywhere web server running a web service.

Prerequisites

This lesson assumes that you have the roles and privileges listed in the Privileges section at the start of this tutorial: Tutorial: Create a web server and access it from a web client.

 Task
  1. Create a SQL Anywhere database that will be used to contain web service definitions.

    dbinit -dba DBA,sql echo
  2. Start a network database server using this database. This server will act as a web server.

    dbsrv16 -xs http(port=8082) -n echo echo.db

    The HTTP web server is set to listen on port 8082 for requests. Use a different port number if 8082 is disallowed on your network.

  3. Connect to the database server with Interactive SQL.

    dbisql -c "UID=DBA;PWD=sql;SERVER=echo"
  4. Create a new web service to accept incoming requests.

    CREATE SERVICE EchoService
    TYPE 'RAW'
    AUTHORIZATION OFF
    SECURE OFF
    USER DBA
    AS CALL Echo();

    This statement creates a new service named EchoService that calls a stored procedure named Echo when a web client sends a request to the service. It generates an HTTP response body without any formatting (RAW) for the web client. If you logged in with a different user ID, then the USER DBA clause must be changed to reflect your user ID.

  5. Create the Echo procedure to handle incoming requests.



    CREATE OR REPLACE PROCEDURE Echo()
    BEGIN
        DECLARE request_body LONG VARCHAR;
        DECLARE request_mimetype LONG VARCHAR;
    
        SET request_mimetype = http_header( 'Content-Type' );
        SET request_body = isnull( http_variable('text'), http_variable('body') );
        IF request_body IS NULL THEN
            CALL sa_set_http_header('Content-Type', 'text/plain' );
            SELECT 'failed'
        ELSE
            CALL sa_set_http_header('Content-Type', request_mimetype );
            SELECT request_body;
        END IF;
    END

    This procedure formats the Content-Type header and the body of the response that is sent to the web client.

Results

A web server is set up to receive requests and send responses.

 See also