How to make 2 query parameters mandatory

aryapr
New Member

I want to make the user enter at least two query parameters otherwise should result in error. Please let me know if there is a way to implement this

0 3 723
3 REPLIES 3

Not applicable

There is a flow variable which counts the number of query parameters.

request.queryparams.count

So use request.queryparams.count < 2 then throw error

If you have SPECIFIC queryparams you want the API Proxy to require, you can use this condition.

<Step>
  <Name>RF-InvalidInput</Name>
  <Condition>request.queryparam.p1 = null OR request.queryparam.p2 = null</Condition>
</Step>

And the RF-InvalidInput policy is a RaiseFault, maybe something like this:

<RaiseFault name='RF-InvalidInput'>
  <IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables>
  <FaultResponse>
    <Set>
      <Payload contentType='application/json'>{
  "error" : {
    "code" : 400.01,
    "message" : "bad request: Missing a required query param."
  }
}
</Payload>
      <StatusCode>400</StatusCode>
      <ReasonPhrase>Bad Request</ReasonPhrase>
    </Set>
  </FaultResponse>
</RaiseFault>


Hey,

Yes , if you just want at least any 2 query parameters than you can use Assign Message policy and raise fault policy to implement the logic.

1) Assign Message will store the parameter count in queryCount :

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<AssignMessage async="false" continueOnError="false" enabled="true" name="AM-QueryParamCount">
    <DisplayName>AM-QueryParamCount</DisplayName>
    <Properties/>
    <AssignVariable>
        <Name>queryCount</Name>
        <Ref>message.queryparams.count</Ref>
    </AssignVariable>
    <IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables>
    <AssignTo createNew="false" transport="http" type="request"/>
</AssignMessage>

2)Raise fault will have the below condition :

So if the value is less than 2 it will trigger the fault.

Condition:

<ProxyEndpoint name="default">
    <PreFlow name="PreFlow">
        <Request>
            <Step>
                <Name>AM-QueryParamCount</Name>
            </Step>
            <Step>
                <Condition>(queryCount<2)</Condition>
                <Name>RF-QueryParam2</Name>
            </Step>
        </Request>
        <Response/>
    </PreFlow>
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<RaiseFault async="false" continueOnError="false" enabled="true" name="RF-QueryParam2">
    <DisplayName>RF-QueryParam2</DisplayName>
    <Properties/>
    <FaultResponse>
        <Set>
            <Headers/>
            <Payload contentType="text/plain"/>
            <StatusCode>400</StatusCode>
            <ReasonPhrase>Bad Request</ReasonPhrase>
        </Set>
    </FaultResponse>
    <IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables>
</RaiseFault>

Hope that this will be helpful to resolve your doubt.