As mentioned in my earlier post there are various ways in which data can passed to a REST service. The cfargument tag has a new attribute restargsource, it can take one of these values – path, query, matrix, form, cookie and header.
Path Param:
Here you can pass arguments to a REST service by specifying the same in the URL path. The URL may look like:
http://localhost:8500/rest/restapp/customerService/CF/Zeus
Here two values ‘CF’ and ‘Zeus’ are passed as an argument to the REST service. The function defined as a REST service in CFC will have two arguments and the functions restpath would have two parameters:
Query Param:
Query params are the most commonly used way to pass data to a REST service by specifying the parameters in URL after question mark(?). The URL would look like this:
http://localhost:8500/rest/restapp/productService?productName=ColdFusion&productCodeName=Zeus
Unlike Path parameters, the name of the argument is specified in the URL itself and the arguments are separated with an ampersand(&). The function to handle this service need not have the restpath attribute defined since the argument is passed as a query parameter in the URL. The other change would be to change the restargsource value to ‘query’.
Matrix Param:
Similar to Query param, in matrix param the arguments are passed to a REST service by specifying same in the URL where the arguments are separated by semicolon(;). Therefore the URL would look something like this:
http://localhost:8500/rest/restapp/productService;productName=ColdFusion;productCodeName=Zeus
Observe that there is no ? symbol used in the URL. Instead a semicolon is used. Here the function providing REST service must have its arguments' restargsource set to ‘matrix’.
Cookie and Header Param:
In case of Cookie and Header param the data is sent in the HTTP request. While sending a REST service request, same can be included by specifying it in the chttpparam tag.
In some cases where the variable name may contain a hyphen(-). For example, the request header may contain variable fname-lname. Now on the server side the argument name cannot be fname-lname because it’s not a valid identifier. To handle this there is an attribute 'restargname'. Here, you can specify the argument name as anything say ‘fname_lname’ and restargname as ‘fname-lname’. The value passed to fname-lname will be mapped to fname_lname.
Form Param:
In many scenarios, data entered in the form has to be processed by a REST service. This data can be used to make a new entry (POST) or update an existing record (PUT) in the database. If form fields are used then the restargsource should be set ‘form’. This will extract the data present in request and will be available for further processing. Also, the content-type header has to be set to ‘application/x-www-form-urlencoded’ when sending data as form fields.
Path Param:
Here you can pass arguments to a REST service by specifying the same in the URL path. The URL may look like:
http://localhost:8500/rest/restapp/customerService/CF/Zeus
Here two values ‘CF’ and ‘Zeus’ are passed as an argument to the REST service. The function defined as a REST service in CFC will have two arguments and the functions restpath would have two parameters:
<cffunction name="pathHandler"
access="remote"
returntype="string"
httpmethod="GET"
produces="text/plain"
restpath="{productName}/{productCodeName}">
<cfargument name="productName"
required="true"
type="string"
restargsource="path"/>
<cfargument name="productCodeName"
required="true"
type="string"
restargsource="path"/>
<cfreturn productName & " " & productCodeName>
</cffunction>
Observe that the restpath attribute in the above function has two values separated by a slash ‘/’ i.e. ‘{productName}/{productCodeName}’. The values ‘CF’ and ‘Zeus’ are copied to these parameters. Another variant would be to pass a hyphen(-) in the restpath in which case the URL would end with CF-Zeus and the function has to updated with its restpath attribute set to {productName}-{productCodeName}.Query Param:
Query params are the most commonly used way to pass data to a REST service by specifying the parameters in URL after question mark(?). The URL would look like this:
http://localhost:8500/rest/restapp/productService?productName=ColdFusion&productCodeName=Zeus
Unlike Path parameters, the name of the argument is specified in the URL itself and the arguments are separated with an ampersand(&). The function to handle this service need not have the restpath attribute defined since the argument is passed as a query parameter in the URL. The other change would be to change the restargsource value to ‘query’.
Matrix Param:
Similar to Query param, in matrix param the arguments are passed to a REST service by specifying same in the URL where the arguments are separated by semicolon(;). Therefore the URL would look something like this:
http://localhost:8500/rest/restapp/productService;productName=ColdFusion;productCodeName=Zeus
Observe that there is no ? symbol used in the URL. Instead a semicolon is used. Here the function providing REST service must have its arguments' restargsource set to ‘matrix’.
Cookie and Header Param:
In case of Cookie and Header param the data is sent in the HTTP request. While sending a REST service request, same can be included by specifying it in the chttpparam tag.
In some cases where the variable name may contain a hyphen(-). For example, the request header may contain variable fname-lname. Now on the server side the argument name cannot be fname-lname because it’s not a valid identifier. To handle this there is an attribute 'restargname'. Here, you can specify the argument name as anything say ‘fname_lname’ and restargname as ‘fname-lname’. The value passed to fname-lname will be mapped to fname_lname.
Form Param:
In many scenarios, data entered in the form has to be processed by a REST service. This data can be used to make a new entry (POST) or update an existing record (PUT) in the database. If form fields are used then the restargsource should be set ‘form’. This will extract the data present in request and will be available for further processing. Also, the content-type header has to be set to ‘application/x-www-form-urlencoded’ when sending data as form fields.
Can you comment on why someone would use "matrix" style? It seems like just an alternate version of regular URL query params.
ReplyDeleteRaymond Camden I don't see any difference other than better readability.
ReplyDeleteThere is difference between matrix and URL params.
ReplyDeleteIn query params, the params always comes at the end after the "?".
http://localhost:8500/rest/restapp/productService?productName=ColdFusion
But in a URI, matrix params can come in between also. For eg:-
http://localhost:8500/rest/restapp/productService;productName=ColdFusion/feature;name=ORM
Yes, as mentioned in the post matrix params use semicolon(;) as a separator and query params use ? and &.
ReplyDeleteAnd you are also right with specifying matrix params anywhere in the URL, but this is not supported in ColdFusion.