How to Access HTTP Variables and Headers Using Web Service Functions

The HTTP_VARIABLE, NEXT_HTTP_VARIABLE, HTTP_HEADER, NEXT_HTTP_HEADER functions can be used to iterate through the variables and headers supplied by the client.

Accessing variables using HTTP_VARIABLE and HTTP_NEXT_VARIABLE

You can iterate through all client-supplied variables using NEXT_HTTP_VARIABLE and HTTP_VARIABLE functions within your stored procedures.

The HTTP_VARIABLE function allows you to get the value of a variable name.

The NEXT_HTTP_VARIABLE function allows you to iterate through all variables sent by the client. Pass the NULL value when calling it for the first time to get the first variable name. Use the returned variable name as a parameter to an HTTP_VARIABLE function call to get its value. Passing the previous variable name to the next_http_variable call gets the next variable name. Null is returned when the last variable name is passed.

Iterating through the variable names guarantees that each variable name is returned exactly once but the variable name order may not be the same as the order they appear in the client request.

The following example illustrates how to use the HTTP_VARIABLE function to retrieve values from parameters supplied in a client request that accesses the ShowDetail service:

CREATE SERVICE ShowDetail
    TYPE 'HTML'
    URL PATH OFF
    AUTHORIZATION OFF
    USER DBA 
    AS CALL ShowDetail();

CREATE PROCEDURE ShowDetail()
BEGIN
    DECLARE v_customer_id LONG VARCHAR;
    DECLARE v_product_id LONG VARCHAR;
    SET v_customer_id = HTTP_VARIABLE( 'customer_id' );
    SET v_product_id = HTTP_VARIABLE( 'product_id' );
    CALL ShowSalesOrderDetail( v_customer_id, v_product_id );
END;

The following example illustrates how to retrieve three attributes from header-field values associated with the image variable:

SET v_name = HTTP_VARIABLE( 'image', NULL, 'Content-Disposition' );
SET v_type = HTTP_VARIABLE( 'image', NULL, 'Content-Type' );
SET v_image = HTTP_VARIABLE( 'image', NULL, '@BINARY' );

Supplying an integer as the second parameter allows you to retrieve additional values. The third parameter allows you to retrieve header-field values from multi-part requests. Supply the name of a header field to retrieve its value.

Accessing headers using HTTP_HEADER and NEXT_HTTP_HEADER

HTTP request headers can be obtained from a request using the NEXT_HTTP_HEADER and HTTP_HEADER functions.

The HTTP_HEADER function returns the value of the named HTTP header field.

The NEXT_HTTP_HEADER function iterates through the HTTP headers and returns the next HTTP header name. Calling this function with NULL causes it to return the name of the first header. Subsequent headers are retrieved by passing the name of the previous header to the function. NULL is returned when the last header name is called.

The following table lists some common HTTP request headers and typical values:

Header name Header value
Accept image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*
Accept-Language en-us
Accept-Charset utf-8, iso-8859-5;q=0.8
Accept-Encoding gzip, deflate
User-Agent Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.2; WOW64; SV1; .NET CLR 2.0.50727)
Host localhost:8080
Connection Keep-Alive

The following table lists special headers and typical values:

Header Name Header Value
@HttpMethod GET
@HttpURI /demo/ShowHTTPHeaders
@HttpVersion HTTP/1.1
@HttpQueryString id=-123&version=109&lang=en

You can use the @HttpStatus special header to set the status code of the request being processed.

The following example illustrates how to format header names and values into an HTML table.

Create the ShowHTTPHeaders web service:

CREATE SERVICE ShowHTTPHeaders
    TYPE 'RAW'
    AUTHORIZATION OFF 
    USER DBA
    AS CALL HTTPHeaderExample();

Create a HTTPHeaderExample procedure that uses the NEXT_HTTP_HEADER function to get the name of the header, then uses the HTTP_HEADER function to retrieve its value:

CREATE PROCEDURE HTTPHeaderExample()
RESULT ( html_string LONG VARCHAR )
BEGIN
    declare header_name LONG VARCHAR;
    declare header_value LONG VARCHAR;
    declare header_query LONG VARCHAR;
    declare table_rows XML;
    set header_name = NULL;
    set table_rows = NULL;
header_loop:        
    LOOP
        SET header_name = NEXT_HTTP_HEADER( header_name );
        IF header_name IS NULL THEN 
            LEAVE header_loop 
        END IF;
        SET header_value = HTTP_HEADER( header_name );
        SET header_query = HTTP_HEADER( '@HttpQueryString' );
        -- Format header name and value into an HTML table row
        SET table_rows = table_rows || 
            XMLELEMENT( name "tr", 
                XMLATTRIBUTES( 'left' AS "align", 
                                'top' AS "valign" ),
                XMLELEMENT( name "td", header_name ),
                XMLELEMENT( name "td", header_value ),
                XMLELEMENT( name "td", header_query ) );
                        
    END LOOP;
    SELECT XMLELEMENT( name "table",
                XMLATTRIBUTES( '' AS "BORDER", 
                             '10' AS "CELLPADDING", 
                              '0' AS "CELLSPACING" ),
                XMLELEMENT( name "th", 
                    XMLATTRIBUTES( 'left' AS "align", 
                                   'top'  AS "valign" ),
                                   'Header Name' ), 
                XMLELEMENT( name "th", 
                    XMLATTRIBUTES( 'left' AS "align", 
                                   'top'  AS "valign" ),
                                   'Header Value' ),
                XMLELEMENT( name "th", 
                    XMLATTRIBUTES( 'left' AS "align", 
                                   'top'  AS "valign" ),
                                   'HTTP Query String' ),
                table_rows );
END;

Access the ShowHTTPHeaders in a web browser to see the request headers arranged in an HTML table.