Default Value for Params

I would like to make the parameters of my API calls not mandatory, for example if i want to implement a page mechanism i would like to able to omit the page parameter if it is not specified and then return the first page.
As I read in the guide it is possible to request parameters from URL Header, but it does not explain if it is possible to give a default value if the data is not collected.
is it possible to implement the mechanism in this code?

nicos97_0-1668072538965.png

 

Solved Solved
0 1 168
1 ACCEPTED SOLUTION

I think you want to apply a default value into a variable, in the case that a queryparam is not passed.  There are three ways to do this.  The first two use AssignMessage/AssignVariable, the last uses the firstnonnull function available within a message template (which can be used within AssignVariable, but also can be used in other places).  Here's an example. 

<AssignMessage name='AM-Variable-with-Defaults'>
  <!-- important -->
  <IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables>

  <!-- option 1: a Value/Ref pair. The Value is used if the Ref is undefined. -->
  <AssignVariable>
    <Name>variable1</Name>
    <Value>1</Value>
    <Ref>request.queryparam.page</Ref>
  </AssignVariable>

  <!-- option 2: a message template with a fallback value (after the colon)-->
  <AssignVariable>
    <Name>variable2</Name>
    <Template>{request.queryparam.page:1}</Template>
  </AssignVariable>

  <!-- option 3: the firstnonnull function -->
  <AssignVariable>
    <Name>default-page</Name>
    <Value>1</Value>
  </AssignVariable>
  <AssignVariable>
    <Name>variable3</Name>
    <Template>{firstnonnull(request.queryparam.page,default-page)}</Template>
  </AssignVariable>

</AssignMessage>

View solution in original post

1 REPLY 1

I think you want to apply a default value into a variable, in the case that a queryparam is not passed.  There are three ways to do this.  The first two use AssignMessage/AssignVariable, the last uses the firstnonnull function available within a message template (which can be used within AssignVariable, but also can be used in other places).  Here's an example. 

<AssignMessage name='AM-Variable-with-Defaults'>
  <!-- important -->
  <IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables>

  <!-- option 1: a Value/Ref pair. The Value is used if the Ref is undefined. -->
  <AssignVariable>
    <Name>variable1</Name>
    <Value>1</Value>
    <Ref>request.queryparam.page</Ref>
  </AssignVariable>

  <!-- option 2: a message template with a fallback value (after the colon)-->
  <AssignVariable>
    <Name>variable2</Name>
    <Template>{request.queryparam.page:1}</Template>
  </AssignVariable>

  <!-- option 3: the firstnonnull function -->
  <AssignVariable>
    <Name>default-page</Name>
    <Value>1</Value>
  </AssignVariable>
  <AssignVariable>
    <Name>variable3</Name>
    <Template>{firstnonnull(request.queryparam.page,default-page)}</Template>
  </AssignVariable>

</AssignMessage>