Variables in HTTP requests come from one of two sources. First, the URL can include a query string, which includes various name=value pairs. HTTP GET requests are formatted in this manner. Here is an example of a URL that contains a query string.
http://localhost/gallery?picture=sunset.jpg |
The second way is through the URL path. Setting URL PATH to either ON or ELEMENTS causes the portion of the path following the service name to be interpreted as variable values. This option allows URLs to appear to be requesting a file in a particular directory, as would be the case on a traditional file-based web site, rather than something stored inside a database. The following is an example.
http://localhost/gallery/sunset.jpg |
This URL appears to request the file sunset.jpg from a directory named gallery. In fact, the gallery service receives this string as a parameter (using it, perhaps, to retrieve a picture from a database table).
The parameter passed in HTTP requests depends on the setting of URL PATH.
OFF No path parameters are permitted after the service name.
ON All path elements after the service name are assigned to the variable URL.
ELEMENTS The remainder of the URL path is split at the slash characters into a list of up to 10 elements. These values are assigned the variables URL1, URL2, URL3, ..., URL10. If there are fewer than 10 values, the remaining variables are set to NULL. Specifying more than ten variables causes an error.
Apart from the location in which they are defined, there is no difference between variables. You access and use all HTTP variables the same way. For example, the values of variables such as url1 are accessed in the same way as parameters that appear as part of a query, such as ?picture=sunset.jpg.
There are two main ways to access variables. The first is to mention variables in the statement of the service declaration. For example, the following statement passes the value of multiple variables to the ShowTable stored procedure:
CREATE SERVICE ShowTable TYPE 'RAW' AUTHORIZATION ON AS CALL ShowTable( :user_name, :table_name, :limit, :start ); |
The other way is to use the built-in functions NEXT_HTTP_VARIABLE and HTTP_VARIABLE within the stored procedure that handles the request. If you do not know which variables are defined, you can use the NEXT_HTTP_VARIABLE to find out. The HTTP_VARIABLE function returns the variable values.
The NEXT_HTTP_VARIABLE function allows you to iterate through the names of the defined variables. The first time you call it, you pass in the NULL value. This returns the name of one variable. Calling it subsequent times, each time passing in the name of the previous variable, returns the next variable name. When the name of the last variable is passed to this function, it returns NULL.
Iterating through the variable names in this manner guarantees that each variable name is returned exactly once. However, the order that the values are returned may not be the same as the order that they appear in the request. In addition, if you iterate through the names a second time, they can be returned in a different order.
To get the value of each variable, use the HTTP_VARIABLE function. The first parameter is the name of the variable. Additional parameters are optional. In the case that multiple values were supplied for a variable, the function returns the first value if supplied with only one parameter. Supplying an integer as the second parameter allows you to retrieve additional values.
The third parameter allows you to retrieve variable header-field values from multi-part requests. Supply the name of a header field to retrieve its value. For example, the following SQL statements retrieve three variable values, then retrieve the header-field values of the image variable.
SET v_id = HTTP_VARIABLE( 'ID' ); SET v_title = HTTP_VARIABLE( 'Title' ); SET v_descr = HTTP_VARIABLE( 'descr' ); |
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' ); |
Here is an example that uses the HTTP_VARIABLE function to retrieve the values associated with the variables. It is a modified version of the ShowSalesOrderDetail service described in an earlier section.
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 service that invokes the stored procedure follows:
CREATE SERVICE ShowDetail TYPE 'HTML' URL PATH OFF AUTHORIZATION OFF USER DBA AS CALL ShowDetail(); |
To test the service, open a web browser and supply the following URL:
http://localhost:80/demo/ShowDetail?product_id=300&customer_id=101
For more information about parameter passing, see Understanding how URLs are interpreted and Web services functions.
Send feedback about this page via email or DocCommentXchange | Copyright © 2008, iAnywhere Solutions, Inc. - SQL Anywhere 11.0.0 |