Quick start to web services

The following procedure describes how to create a new database, start a SQL Anywhere database with the HTTP server enabled, and access this database using any popular web browser.

To create and access a simple HTML web service
  1. At a command prompt, run the following command to start a personal web server. Replace samples-dir with the actual location of the sample database. The -xs http(port=80) option tells the database server to listen for HTTP requests. If you already have a web server running on port 80, use another port number such as 8080 for this demonstration.

    dbeng11 -xs http(port=80) samples-dir\demo.db

    Many properties of the HTTP communication link are controlled by parameters to the -xs option. See -xs server option.

  2. Start the database server with the appropriate -xs option parameters and use the CREATE SERVICE statement to create web services to respond to incoming requests.

    Start Interactive SQL. Connect to the SQL Anywhere sample database as the DBA. Execute the following statement.

    CREATE SERVICE HTMLtable
    TYPE 'HTML'
    AUTHORIZATION OFF
    USER DBA
    AS SELECT * FROM Customers;

    This statement creates a web service named HTMLtable. This simple web service returns the results of the statement SELECT * FROM Customers, automatically converting the output into HTML format. Because authorization is off, no permissions are required to access the table from a web browser. See Creating web services and CREATE SERVICE statement.

  3. Start a web browser.

  4. Browse to the URL [external link] http://localhost:80/demo/HTMLtable. Use the port number you specified when starting the database server.

    Your web browser shows you the body of the HTML document returned by the database server. By default, the result set is formatted into an HTML table.

To create and access a simple XML web service
  1. At a command prompt, run the following command to start a personal web server. Replace samples-dir with the actual location of the sample database. The -xs http(port=80) option tells the database server to listen for HTTP requests. If you already have a web server running on port 80, use another port number such as 8080 for this demonstration.

    dbeng11 -xs http(port=80) samples-dir\demo.db

    Many properties of the HTTP communication link are controlled by parameters to the -xs option. See -xs server option.

  2. Start the database server with the appropriate -xs option parameters and use the CREATE SERVICE statement to create web services to respond to incoming requests.

    Start Interactive SQL. Connect to the SQL Anywhere sample database as the DBA. Execute the following statement:

    CREATE SERVICE XMLtable
    TYPE 'XML'
    AUTHORIZATION OFF
    USER DBA
    AS SELECT * FROM Customers;

    This statement creates a web service named XMLtable. This simple web service returns the results of the statement SELECT * FROM Customers, automatically converting the output into XML format. Because authorization is off, no permissions are required to access the table from a web browser. See Creating web services and CREATE SERVICE statement.

  3. Start a web browser.

  4. Browse to the URL [external link] http://localhost:80/demo/XMLtable. Use the port number you specified when starting the database server.

    • localhost:80 Defines the web host name and port number to use.

    • demo Defines the database name to use. You are using demo.db.

    • XMLtable Defines the service name to use.

    Your web browser shows you the body of the XML document returned by the database server. As no formatting information has been included, you see the raw XML, including tags and attributes.

  5. You can also access the XMLtable service from common programming languages. For example, the following short C# program uses the XMLtable web service:

    using System.Xml;
    
    static void Main(string[] args)
    {
      XmlTextReader reader =
        new XmlTextReader( "http://localhost:80/demo/XMLtable" );
    
      while( reader.Read() )
      {
        switch( reader.NodeType )
        {
        case XmlNodeType.Element:
          if( reader.Name == "row" )
          {
            Console.Write(reader.GetAttribute("ID")+" ");
            Console.WriteLine(reader.GetAttribute("Surname"));
          }
          break;
        }
      }
      reader.Close();
    }
  6. In addition, you can access the same web service from Python, as in the following example:

    import xml.sax
    
    class DocHandler( xml.sax.ContentHandler ):
      def startElement( self, name, attrs ):
        if name == 'row':
          table_id = attrs.getValue( 'ID' )
          table_name = attrs.getValue( 'Surname' )
          print '%s %s' % ( table_id, table_name )
    
    parser = xml.sax.make_parser()
    parser.setContentHandler( DocHandler() )
    parser.parse( 'http://localhost:80/demo/XMLtable' )

    Save this code in a file called DocHandler.py. To run the application, enter a command like the following:

    python DocHandler.py
To create and access a simple JSON web service
  1. At a command prompt, run the following command to start a personal web server. Replace samples-dir with the actual location of the sample database. The -xs http(port=80) option tells the database server to listen for HTTP requests. If you already have a web server running on port 80, use another port number such as 8080 for this demonstration.

    dbeng11 -xs http(port=80) samples-dir\demo.db

    Many properties of the HTTP communication link are controlled by parameters to the -xs option. See -xs server option.

  2. Start the database server with the appropriate -xs option parameters, and use the CREATE SERVICE statement to create web services to respond to incoming requests.

    Start Interactive SQL. Connect to the SQL Anywhere sample database as the DBA. Execute the following statement:

    CREATE SERVICE JSONtable
    TYPE 'JSON'
    AUTHORIZATION OFF
    USER DBA
    AS SELECT * FROM Customers;

    This statement creates a web service named JSONtable. This simple web service returns the results of the statement SELECT * FROM Customers, automatically converting the output into JavaScript Object Notation format. Because authorization is off, no permissions are required to access the table from a web browser. See Creating web services and CREATE SERVICE statement.

  3. Start a web browser.

  4. Browse to the URL [external link] http://localhost:80/demo/JSONtable. Use the port number you specified when starting the database server.

    • localhost:80 Defines the web host name and port number to use.

    • demo Defines the database name to use. You are using demo.db.

    • JSONtable Defines the service name to use.

    Your web browser should permit you to save the JSON response document returned by the database server. Save the response to a file.

  5. If you use a text editor to view the file containing the response, you will see the following array notation for the result set.

    [
            {
                    "ID": 101,
                    "Surname": "Devlin",
                    "GivenName": "Michaels",
                    "Street": "114 Pioneer Avenue",
                    "City": "Kingston",
                    "State": "NJ",
                    "Country": "USA",
                    "PostalCode": "07070",
                    "Phone": "2015558966",
                    "CompanyName": "The Power Group"
            },
            {
                    "ID": 102,
                    "Surname": "Reiser",
                    "GivenName": "Beth",
                    "Street": "33 Whippany Road",
                    "City": "Rockwood",
                    "State": "NY",
                    "Country": "USA",
                    "PostalCode": "10154",
                    "Phone": "2125558725",
                    "CompanyName": "AMF Corp."
            },
    .
    .
    .
            {
                    "ID": 665,
                    "Surname": "Thompson",
                    "GivenName": "William",
                    "Street": "19 Washington Street",
                    "City": "Bancroft",
                    "State": "NY",
                    "Country": "USA",
                    "PostalCode": "11700",
                    "Phone": "5165552549",
                    "CompanyName": "The Apple Farm"
            }
    ]
Other resources for getting started

Samples are included in the samples-dir\SQLAnywhere\HTTP directory.

Other examples might be available on CodeXchange at [external link] http://www.sybase.com/developer/codexchange.

See also