WSOptional
Qualifies a parameter as optional in a REST function.
Syntax
WSOptional
WSOptional
is an optional attribute.
Usage
You can set
WSOptional
on the ATTRIBUTE()
clause of a parameter
when a function does not require the parameter to be present when a client calls the service.
Setting parameters as optional is typically used when defining REST functions in the following
situations: - If a query string (
WSQuery
) is not always required, set the input parameter withWSOptional
. For an example, see Example use of parameter attributes. - If a custom header (
WSHeader
), input or output, is not always required, set the parameter withWSOptional
. For an example, see Example use of parameter attributes. - If a cookie (
WSCookie
) is not always required, set the parameter withWSOptional
. See Example: Optional cookie.
Example: Optional cookie
IMPORT com
TYPE profileType RECORD
id INT,
name VARCHAR(50),
email VARCHAR(100),
ccode VARCHAR(10)
# ...
END RECORD
DEFINE myError RECORD ATTRIBUTE(WSError="My error")
code INTEGER,
reason STRING
END RECORD
PUBLIC FUNCTION getUsersByCountry(
p_country STRING ATTRIBUTE(WSCookie, WSOptional, WSName = "Preferred_country" ) )
ATTRIBUTES (WSGet,
WSPath="/users",
WSDescription="Get users by country if the cookie is sent by client.",
WSThrows="400:Invalid,404:NotAvailable" )
RETURNS (
DYNAMIC ARRAY ATTRIBUTE(WSName="Users",WSMedia="application/xml,application/json") OF profileType
)
DEFINE countryList DYNAMIC ARRAY OF profileType
IF p_country IS NULL THEN
SELECT * FROM users
ELSE
SELECT * FROM users WHERE @ccode=p_country
END IF
# ... function code ...
RETURN countryList
END FUNCTION