Working with MIME types

The TYPE clause for a SQL Anywhere web service client procedure or function definition allows the specification of a MIME type. The value of the MIME type specification is used to set the Content-Type request header and set the mode of operation to allow only a single call parameter to populate the body of the request. Only zero or one parameter may remain when making a web service stored procedure (or function) call after parameter substitutions have been processed. Calling a web service procedure with a null or no parameter (after substitutions) will result in a request with no body and a content-length of zero. The behavior has not changed if a MIME type is not specified. Parameter names and values (multiple parameters are permitted) are URL encoded within the body of the HTTP request.

Some typical MIME types include:

The following steps illustrate the setting of a MIME type. The first part sets up a web service that can be used to test the setting of MIME type. The second part demonstrates how to set a MIME type.

Create a web service server

  1. Create a database.

    dbinit echo
  2. Start a server using this database.

    dbsrv11 -xs http(port=8082) -n echo echo.db
  3. Connect to the server using Interactive SQL.

    dbisql -c "UID=DBA;PWD=sql;ENG=echo"
  4. Using Interactive SQL, create a web service.

    CREATE SERVICE EchoService
    TYPE 'RAW'
    USER DBA
    AUTHORIZATION OFF
    SECURE OFF
    AS CALL Echo(:valueAsXML);
  5. Define the stored procedure that this service is to call.

    CREATE PROCEDURE Echo( parm LONG VARCHAR )
    BEGIN
        SELECT parm; 
    END;

At this point, you now have a SQL Anywhere web service server running and ready to handle requests. The server is listening for HTTP requests on port 8082.

To use this web server for testing, create another SQL Anywhere database, start it, and connect to it. The following steps show how to do this.

To send an HTTP request

  1. Using the database creation utility, create another database for use with a web service client.

    dbinit echo_client
  2. Continuing with Interactive SQL, start this database using the following statement.

    START DATABASE 'echo_client.db' 
    AS echo_client;
  3. Now, connect to the database that has been started on the server echo using the following statement.

    CONNECT TO 'echo'
    DATABASE 'echo_client' 
    USER 'DBA' 
    IDENTIFIED BY 'sql';
  4. Create a stored procedure that will communicate with the EchoService web service.

    CREATE PROCEDURE setMIME( 
      value LONG VARCHAR, 
      mimeType LONG VARCHAR, 
      urlSpec LONG VARCHAR
      )
    URL '!urlSpec'
    HEADER 'ASA-Id'
    TYPE 'HTTP:POST:!mimeType';

    The URL clause is used to reference the web service. For illustration purposes, the URL will be passed as a parameter to the setMIME procedure.

    The TYPE clause indicates that the MIME type will be passed as a parameter to the setMIME procedure. The default format used when making a web service request is 'SOAP:RPC'. The format chosen for making this web service request is 'HTTP:POST'.

  5. Call the stored procedure in order to send the request and obtain the response. The value parameter that is passed is a URL-encoded form of <hello>this is xml</hello>. The media type is application/x-www-form-urlencoded since form-urlencoded is understood by the SQL Anywhere web server. The URL for the web service is included as the final parameter in the call.

    CALL setMIME('valueAsXML=%3Chello%3Ethis%20is%20xml%3C/hello%3E', 
        'application/x-www-form-urlencoded', 
        'http://localhost:8082/EchoService');

    The final parameter specifies the URI of the web service that is listening on port 8082.

The following is representative of the HTTP packet that is sent to the web server.

POST /EchoService HTTP/1.0
Date: Sun, 28 Jan 2007 04:04:44 GMT
Host: localhost
Accept-Charset: windows-1252, UTF-8, *
User-Agent: SQLAnywhere/11.0.0.1297
Content-Type: application/x-www-form-urlencoded; charset=windows-1252
Content-Length: 49
ASA-Id: 1055532613:echo_client:echo:968000
Connection: close

valueAsXML=%3Chello%3Ethis%20is%20xml%3C/hello%3E

The following is the response from the web server.

HTTP/1.1 200 OK
Server: SQLAnywhere/11.0.0.1297
Date: Sun, 28 Jan 2007 04:04:44 GMT
Expires: Sun, 28 Jan 2007 04:04:44 GMT
Content-Type: text/plain; charset=windows-1252
Connection: close

<hello>this is xml</hello>

The result set that is displayed by Interactive SQL is shown next.

Attribute Value
Status HTTP/1.1 200 OK
Body <hello>this is xml</hello>
Server SQLAnywhere/11.0.0.1297
Date Sun, 16 Dec 2007 04:04:44 GMT
Expires Sun, 16 Dec 2007 04:04:44 GMT
Content-Type text/plain; charset=windows-1252
Connection close