Assign default value to query parameters

Not applicable

I am trying to assign a default value of the query parameter request.queryparam.results

I am using the query parameter in callouts but get 500 responses when request.queryparam.results=null.

If I use 'set' in Assign Message Policy I overwrite the value which is not what I won't if it is different from null.

How can I set a default value of the parameter?

Solved Solved
2 9 4,668
2 ACCEPTED SOLUTIONS

I really like using a simple javascript callout for this sort of thing. The ternary operator makes it pretty easy to one-line this.

context.setVariable('request.queryparam.results', context.getVariable('request.queryparam.results') ? context.getVariable('request.queryparam.results') : 0)

View solution in original post

Not applicable

This works for me in a single Assign Message and for multiple params @Anders Bilfeldt @Carlos Eberhardt

<AssignMessage async="false" continueOnError="false" enabled="true" name="Assign-Default-Query-Params">
    <DisplayName>Assign Default Query Params</DisplayName>
    <FaultRules/>
    <Properties/>
    <AssignVariable>
        <Name>request.queryparam.format</Name>
      	<Value>json</Value>
      	<Ref>request.queryparam.format</Ref>
    </AssignVariable>
    <AssignVariable>
        <Name>request.queryparam.offset</Name>
      	<Value>0</Value>
      	<Ref>request.queryparam.offset</Ref>
    </AssignVariable>
    <AssignVariable>
        <Name>request.queryparam.limit</Name>
      	<Value>10</Value>
      	<Ref>request.queryparam.limit</Ref>
    </AssignVariable>
...

View solution in original post

9 REPLIES 9

I really like using a simple javascript callout for this sort of thing. The ternary operator makes it pretty easy to one-line this.

context.setVariable('request.queryparam.results', context.getVariable('request.queryparam.results') ? context.getVariable('request.queryparam.results') : 0)

You can also do it with ||, which works similarly. Example.

context.setVariable('request.queryparam.format',
   context.getVariable('request.queryparam.format') || 'json'); 

This works because of the "truthiness" of Javascript variables. Even so, I would prefer to avoid an assignment unless it is necessary. So,

var varName = 'request.queryparam.format';
if (!context.getVariable(varName)) {context.setVariable(varName, 'json');}

Not applicable

Thank you, that would work just fine 🙂 I am wondering if it is possible to do it using "Assign Message Policy"?

Not applicable

you can use condition before invoking the Assign Message policy.

some thing like below

<Step>AssignMessage</Step>

<Condition>(request.queryparam.results = null) and

(request.queryparam.results = "")</Condition>

I hadn't thought of that. However, I believe the operator should be != instead of =

That is a nice solution, but lets sat I have three query paramters that must have default values, then I must create three Assign Message Policies 😞

Not applicable

This works for me in a single Assign Message and for multiple params @Anders Bilfeldt @Carlos Eberhardt

<AssignMessage async="false" continueOnError="false" enabled="true" name="Assign-Default-Query-Params">
    <DisplayName>Assign Default Query Params</DisplayName>
    <FaultRules/>
    <Properties/>
    <AssignVariable>
        <Name>request.queryparam.format</Name>
      	<Value>json</Value>
      	<Ref>request.queryparam.format</Ref>
    </AssignVariable>
    <AssignVariable>
        <Name>request.queryparam.offset</Name>
      	<Value>0</Value>
      	<Ref>request.queryparam.offset</Ref>
    </AssignVariable>
    <AssignVariable>
        <Name>request.queryparam.limit</Name>
      	<Value>10</Value>
      	<Ref>request.queryparam.limit</Ref>
    </AssignVariable>
...

Not applicable

Yes, you can do this with assignMessage policy. Just wrap the condition around assignMessage Policy in the flow to make sure AssignMessage policy is executed only when the value of query param is either null or blank.

Like the following:

<Flow name="Blah"> <Description/> <Request> <Step> <Condition>(request.queryparam.results == null) OR (request.queryparam.results == "")</Condition> <Name>Provide-default-queryparam</Name> </Step>

</Flow>

In AssignMesage policy, just set the queryparam as shown above.

Thank you so much, this worked perfectly!