Request Parameters

This section describes processing of parameters received from the HTTP request. How to make request with parameters has been discussed here.

Before proceeding to the following article, the reader must be familiar with the following

Accessing request parameters

HTTP request parameters can be accessed anywhere inside <Request>. The name of the parameter must be prefixed with $ symbol to access it.

Parameters inside Query/Update

In the following example, the request parameter $department (query parameter in GET request) is accessed inside <Query>.

<Query>
    SELECT name,age,location FROM emp WHERE dept=$department
</Query>

Parameter Validation

Parameters can be validated using <Param> inside <Request>

<Resource xmlns="http://xml.metamug.net/resource/1.0" v="1.0">
    <Request method="GET">
        <Desc>
            Request to display a single page. 
            User needs to calculate offset = limit + offset 
            for each subsequent request. 
            Offset is 900 i.e There are 1000 records.
            Limit being 100, Page size cannot be more than 100.
        </Desc>
        <Param name="limit" type="number" min="0" max="100"/>
        <Param name="offset" type="number" min="0" max="900"/>
        <Query requires="limit,offset" limit="limit" offset="offset">
            SELECT * FROM tbl_task_master 
        </Query>
    </Request>
</Resource>

Type Attribute

Here type is not only identifying the type of the variable but also cause a validation error if the value sent isn't the same type. For example, the type number can have floating point and integer numbers. The value for supported types are:

Validation Attributes

Attribute Description
min Minimum value of the parameter. Works only with type number.
max Maximum value of the parameter. Works only with type number.
minlength Minimum length of the parameter string.
maxlength Maximum length of the parameter string.
pattern Regular Expression to match the parameter string.
value The default value of the parameter, in case it is not sent.
required Makes sending the parameter mandatory.

Requires Attribute (Query/Update)

The requires attributes help identify the system that certain parameters are needed for the query to be executed. Typically, these parameters are used in the where clause of the query in "Query" tag. And as insert values, when performing an insert operation in "Update" tag.

The parameters required by the SQL statement are listed in this attribute, separated by commas. For e.g., <Query requires="name,age,phone,salary" >

Pagination Parameters

The query executed can be paginated using limit and offset attribute on query tag, as seen in the example above (page.xml). These attributes take parameter names as value. When the query is executed on the server, the offset and limit are applied to the query defined by these attributes.

<Param name="limit" type="number" min="0" max="100"/>
<Param name="offset" type="number" min="0" max="900"/>
<Query requires="limit,offset" limit="limit" offset="offset">
    SELECT * FROM tbl_task_master
</Query>

Suppose this query SELECT * FROM tbl_task_master gives you 100 records but you only want show 3 records. This can be done with the help of query parameters limit and offset.

Limit parameter reduces the records whereas the offset help's you to set from which record you would like to display data from the 100 records.

http://localhost:7000/scheduler/v1.0/task?offset=11&limit=3

The above API call will give you 3 results starting from 11.

If you want to update the offset for the next page it will be offset = offset + limit i.e offset = 11 + 3 so the offset would be 14 and limit will be 3.

Item ID

The variable $id is reserved for id of the resource in case of an item request. Let us consider a GET request made to /employees/2. Here, the item id is 2 and it can be accessed in the resource as follows

<Query>
    SELECT * FROM employee WHERE id=$id
</Query>