Is it possible to validate queryparam with no value using Apigee Conditional Statements in request uri ?

I would like to check whether a queryparam exist or not in request uri.

For example how can i validate a queryparam 'company' exists or not in below request uri using apigee conditional statement ?

http://www.example.com?name=anil&company

PS: One way is to use regular expression, other way is to use javascript policy.

Is it possible to validate this using variable and conditional operators ?

Cheers,

Anil Sagar

Solved Solved
0 9 5,232
1 ACCEPTED SOLUTION

@Anil Sagar - request.queryparam.company will give you null if query parameter's value is empty (perhaps a bug in the product).

However you will get the existence of the queryparam using request.queryparams.names. It will give you an array which contains [company]

View solution in original post

9 REPLIES 9

sgilson
Participant V

One option is to use the AssignMessage policy to assign a default value to the query param if the value is omitted:

<AssignMessage async="false" continueOnError="false" enabled="true" name="DefaultWQP">
  <AssignTo createNew="false" transport="http" type="request"/>
  <IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables>
  <AssignVariable> 
    <Name>request.queryparam.company</Name>
    <Ref>request.queryparam.comapny</Ref> 
    <Value>Apigee</Value>
  </AssignVariable> 
</AssignMessage>

@sgilson

, Does above policy executes even if key is not present ? Ideally i would like to detect whether a key exists without a value. Something like check variable is defined or not even though it doesn't have any value

It does. You could set the default value to something no one would enter, like "xxxyyyzzz", and then check request.queryparam.comapny against that value.

@sgilson - in this scenario, your assign message will always override as request.queryparam.company will always be null.

A queryparam variable value will always be null unless it has a value.

Right - I was saying that this will set the query param to "xxxyyyzzz" only when the query param is null. If the user passes a value for it in the request, then the query param will be set as specified by the user.

This will set the query param to "xxxyyyzzz" successfully in two different cases which Anil is trying to distinguish between:

api.com

api.com?company

He is trying to understand whether company parameter exists or not.

@Anil Sagar - request.queryparam.company will give you null if query parameter's value is empty (perhaps a bug in the product).

However you will get the existence of the queryparam using request.queryparams.names. It will give you an array which contains [company]

Thank you @Ozan Seymen.... I just thought the same. Hopefully it will be fixed in next few releases if that's the case.

Not applicable

The following tests for values in a queryparam and raises a fault only if the value is null - essentially executing the raiseFault policy if the queryparam were omitted:

<Step>
	<Name>raiseBadRequest</Name>
	<Condition>request.queryparam.limit==null</Condition>
</Step>

This can be inverted to execute a policy if the value were defined (!=null).