Lesson 2: Sending Requests from a Web Client and Receiving Responses

In this lesson, you set up a database client to send requests to a web server using the POST method and to receive the web server's responses.

Prerequisites

This lesson assumes that you have set up a web server as instructed in Lesson 1.

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

This lesson contains several references to localhost. Use the host name or IP address of the web server from Lesson 1 instead of localhost if you are not running the web client on the same computer as the web server.

  1. Create an SAP Sybase IQ database that will be used to contain web client procedures.
    iqinit -dba <user_id>,<password> echo_client
  2. Start a network database server using this database. This server will act as a web client.
    iqsrv16 echo_client.db
  3. Connect to the database server with Interactive SQL.
    dbisql -c "UID=<user_id>;PWD=<password>;SERVER=echo_client"
  4. Create a new stored procedure to send requests to a web service.
    CREATE OR REPLACE PROCEDURE SendWithMimeType( 
        value LONG VARCHAR, 
        mimeType LONG VARCHAR, 
        urlSpec LONG VARCHAR
    )
    URL '!urlSpec'
    TYPE 'HTTP:POST:!mimeType';

    The SendWithMimeType procedure has three parameters. The value parameter represents the body of the request that should be sent to the web service. The urlSpec parameter indicates the URL to use to connect to the web service. The mimeType indicates which MIME type to use for the HTTP:POST.

  5. Send a request to the web server and obtain the response.
    CALL SendWithMimeType('<hello>this is xml</hello>', 
        'text/xml', 
        'http://localhost:8082/EchoService'
    );

    The http://localhost:8082/EchoService string indicates that the web server runs on localhost and listens on port 8082. The desired web service is named EchoService.

  6. Try a different MIME type and observe the response.
    CALL SendWithMimeType('{"menu": {  "id": "file", "value": "File", "popup": {
        "menuitem": [{"value": "New", "onclick": "CreateNew()"},
                     {"value": "Open", "onclick": "Open()"},
                     {"value": "Close", "onclick": "Close()"} ] } } }', 
        'application/json', 
        'http://localhost:8082/EchoService'
    );

A web client is set up to send HTTP requests to a web server using the POST method and receive the web server's response.