WSCookie
Allows cookies to be passed to Web service REST function.
Syntax
WSCookie
Usage
You set this attribute to indicate that a parameter value will transport a cookie. The client application needs to provide the appropriate header and value when making a call to the function.
WSCookie
is defined in an ATTRIBUTE()
clause of an input
parameter of the function.
WSCookie
is an optional attribute.
Example WSCookie
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 getUsersByMyCountry(
p_country STRING ATTRIBUTE(WSCookie, WSOptional, WSName = "Preferred_country" ) )
ATTRIBUTES (WSGet,
WSPath="/users",
WSDescription="Get users with cookie (for country) if sent from 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
INITIALIZE countryList TO NULL
WHENEVER ERROR CONTINUE
IF p_country IS NULL THEN
DECLARE c1 CURSOR FOR SELECT * FROM users
ELSE
DECLARE c2 CURSOR FOR SELECT * FROM users WHERE @ccode=p_country
END IF
WHENEVER ERROR STOP
CASE
WHEN SQLCA.SQLCODE = 0
# ... function code ...
WHEN SQLCA.SQLCODE = NOTFOUND
LET myError.reason = "No users found"
CALL com.WebServiceEngine.SetRestError(400,myError)
OTHERWISE
LET myError.reason = SFMT("SQL error:%1 [%2]",SQLCA.SQLCODE, SQLERRMESSAGE)
CALL com.WebServiceEngine.SetRestError(404,myError)
END CASE
FREE c1
FREE c2
RETURN countryList
END FUNCTION
In the call to the function, a header named "Preferred_country" provides the cookie value.