How to handle optional query params?

Not applicable

Hi,

I am using ExtractVariables and then AssignMessage Policy to set the path, query params and header in the request from the extracted variables.

Now consider a scenario where i have a query param being set in the AssignMessage Policy but it is optional. It works fine if i pass the query param in request. It sends a empty param (only key, no value) if i do not pass in request. However, the behavior i want is that the query param should not be passed.

How to handle such a scenario?

0 5 2,744
5 REPLIES 5

@rohan kapoor , How about using another "Assign Message policy" which will conditionally execute if query param is empty & then remove the same.

Hope it helps.

Thanks for your help Anil.

I tried a similar work around now and it worked. Used a java script policy after AssignMessage which removes the query param if the variable extracted in ExtractVariables is null.

Something like (where urirequest is prefix in ExtractVariables step):

var status = context.getVariable("urirequest.status")

if(status === null) context.removeVariable("request.queryparam.status")

All this while I was wondering if we can do this without adding another step.

@rohan kapoor , Actually, You can do it using policy conditions & two different "Assign Message" policies where one of them executes based on query param value. You can avoid executing two policies. Hope it helps. I would not prefer JS Policy when you can achieve same using OOB policies like Assign Message Policy. Hope it helps.

Hello @anilsr , i am trying to implement a similar thing. Can you please help with how can I use another message policy to execute based on a particular query parameter?  Thanks in advance.!

how can I use another message policy to execute based on a particular query parameter?

your question is not clear. Can you possibly provide some more context?

Maybe what you mean to ask is... how can i execute a policy step conditionally, based on the presence of an optional query parameter? If that's what you mean, then it's a matter of wrapping the Step in a Condition, within the flow.

 

 

  <Flow> 
    <Request>
      <Step> 
        <!-- conditionally execute a step based on presence of a query param -->
        <Name>POLICY-NAME-HERE</Name>
        <Condition>NOT(request.queryparam.something = null)</Condition>
      </Step>
      ...

 

 

Also please ask new questions with the "Ask a Question" button. If you like you can refer to THIS question in your new question, if it provides some helpful context.

ask-a-new-question.png