Add condition in Assign Message

here's my code

What I don't undrstand is: my conditio is true, so the ADD cord is supposed to be SET. Unfortunately it doesn't work.

I set my variable: addingCors = true in a JS script before calling this Assign Message.

So is it possible to add conditions in a SET ?

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<AssignMessage async="false" continueOnError="false" enabled="true" name="add-cors">
    <DisplayName>Add CORS</DisplayName>
    <FaultRules/>
    <Properties/>
    <Set>
        <Step>
            <Headers>
                <Header name="Access-Control-Allow-Origin">{header-origin}</Header>
                <Header name="Access-Control-Allow-Headers">origin, x-requested-with, accept, content-type</Header>
                <Header name="Access-Control-Max-Age">3628800</Header>
                <Header name="Access-Control-Allow-Methods">GET, PUT, POST, DELETE</Header>
            </Headers>
            <Condition>(addingCors = true)</Condition>
        </Step>
    </Set>
    <IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables>
    <AssignTo createNew="false" transport="http" type="response"/>
</AssignMessage>
Solved Solved
0 4 2,701
1 ACCEPTED SOLUTION

You cannot add a Condition element under a Set element within AssignMessage.

Also, you have a stray <Step> element under Set. That shouldn't be there.

I think you want this:

<AssignMessage name="add-cors">
    <Set>
            <Headers>
                <Header name="Access-Control-Allow-Origin">{header-origin}</Header>
                <Header name="Access-Control-Allow-Headers">origin, x-requested-with, accept, content-type</Header>
                <Header name="Access-Control-Max-Age">3628800</Header>
                <Header name="Access-Control-Allow-Methods">GET, PUT, POST, DELETE</Header>
            </Headers>
    </Set>
    <IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables>
</AssignMessage>

If you attach it in the Response flow, then you don't need AssignTo either.

The Step and Condition apply to flow configuration.

The AssignMessage is not a flow; it's a policy.

I would expect the proxy editor to flag your use of Step and Condition within AssignMessage as a violation of the schema. But I'm not sure that happens.

View solution in original post

4 REPLIES 4

You cannot add a Condition element under a Set element within AssignMessage.

Also, you have a stray <Step> element under Set. That shouldn't be there.

I think you want this:

<AssignMessage name="add-cors">
    <Set>
            <Headers>
                <Header name="Access-Control-Allow-Origin">{header-origin}</Header>
                <Header name="Access-Control-Allow-Headers">origin, x-requested-with, accept, content-type</Header>
                <Header name="Access-Control-Max-Age">3628800</Header>
                <Header name="Access-Control-Allow-Methods">GET, PUT, POST, DELETE</Header>
            </Headers>
    </Set>
    <IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables>
</AssignMessage>

If you attach it in the Response flow, then you don't need AssignTo either.

The Step and Condition apply to flow configuration.

The AssignMessage is not a flow; it's a policy.

I would expect the proxy editor to flag your use of Step and Condition within AssignMessage as a violation of the schema. But I'm not sure that happens.

Thanks. Your explanation helped me. Very clear.

I've added my condition in the flow that calls the AssignMessage !!!

My AssignMessage is how you described it. And I add my condition in the flow.

Now it works.

But what if we want to add headers conditionally?

 

Wrap a condition around the step, in the flow. 

<Flow name='flow-1'> 
  <Condition>proxy.pathsuffix ~ "/foobar"</Condition>
  <Request> 
    <Step>
      <Name>AM-SetHeaders</Name>
      <Condition>CONDITION_HERE</Condition>
    </Step>
  ...