0

I know that in ColdFusion, making the access level of a function remote means that all arguments are required. How do I correctly deal with optional arguments? Often we are getting requests sent to these functions with:

?method=doSomething&requiredArg="something"&optionalArg1=&optionalArg2= 

Is the only way to deal with this making the type of the argument in the function "any" or "string" and doing type checking in the code? I'm considering building an master "remoteApiCFC" that does this with a function and running it on every remote function to get a local.arguments struct. Is there a better way?

James A Mohler
  • 11,060
  • 15
  • 46
  • 72
bau
  • 21
  • 3
  • 2
    How are you invoking the CFC? It would help to see some code. Using "remote" doesn't make function arguments required [unless it's used as a web service](https://helpx.adobe.com/coldfusion/cfml-reference/coldfusion-tags/tags-a-b/cfargument.html) and there's no `default` value defined for the argument: *...All arguments are required when invoked as a web service, irrespective of how they are defined.Specifies whether the parameter is required to execute the component method. The parameter is not required if you specify a default attribute.* – SOS Jun 13 '19 at 17:21

1 Answers1

1

This is how you define optional arguments for a method:

remote string function foo (
    required string bar,
    string option1="optional") {

    return(bar & option1);
}
Redtopia
  • 4,947
  • 7
  • 45
  • 68