AssignMessage : substring not working

karliso
Participant II

hello, I have the below policy which is accessing a variable. I am trying to use the substring function in the policy to take the first 5 charters of the string and forward it in the header. For whatever reason substring seems to be doing nothing. Any ideas on why this is? to lower works just fine, but I can't seem to figure out substring. Thanks.

<?xml version="1.0" encoding="UTF-8" standalone="yes"?><AssignMessage async="false" continueOnError="false" enabled="true" name="forward-user-info">
<DisplayName>forward-user-info</DisplayName>
<Set>
<Headers>
<Header name="x-app-name">{substring(dev.app-name,0,4)}</Header>
</Headers>
</Set>
</AssignMessage>
Solved Solved
0 14 1,395
1 ACCEPTED SOLUTION

Try it with IgnoreUnresolvedVariables = true.

<AssignMessage name='AM-Substring'>
  <DisplayName>AM-Substring</DisplayName>


  <!-- this element is important because Apigee attempts to
       treat the 0 and 4 as variables, which will be unresolved.
       :(


       But we just want them to be numbers.
       -->


  <IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables>


  <AssignVariable>
    <Name>assigned</Name>
    <Template>{substring(alphabet,0,4)}</Template>
  </AssignVariable>


</AssignMessage>


View solution in original post

14 REPLIES 14

sidd-harth
Participant V

The substring functionality is working perfectly fine for me.

Check your Trace, to see the actual value of dev.app-name

Download and provide the Trace here.

There is definatly a value there as the app name gets added to the header, but what doesn't happen is substring take the first 4 characters. The whole value is being forwarded in the header. Let me play play with it some more.

do you have a sample test proxy with this working?

Are you using Apigee SaaS?

No, this is in Apigee edge

Try it with IgnoreUnresolvedVariables = true.

<AssignMessage name='AM-Substring'>
  <DisplayName>AM-Substring</DisplayName>


  <!-- this element is important because Apigee attempts to
       treat the 0 and 4 as variables, which will be unresolved.
       :(


       But we just want them to be numbers.
       -->


  <IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables>


  <AssignVariable>
    <Name>assigned</Name>
    <Template>{substring(alphabet,0,4)}</Template>
  </AssignVariable>


</AssignMessage>


Interestingly that doesn't work. But what does work is taking your policy substringing the one variable I want to substring and saving the result to 'assigned' as your policy shows. I then access 'assigned' in my policy and it is substring as needed. If I try to use substring in my policy it does not work. For the life of my I can't figure out why. Thanks, that keeps me moving forward.

So you're saying "what you showed me doesn't work" and you're also saying "what you showed me works."

??

Glad to help.

figured out why what I was doing was failing. I had two sets of 'headers' tags which caused substring to fail:

<Set><Headers><Header name="x-app-name">{dev.app-name}</Header></Headers><Headers><Header name="x-client-id-prefix">{substring(client_id, 0,4)}</Header></Headers></Set>

With one 'headers' tag it works fine.

chawkimatta
Participant III

did you try to add <IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables> in your policy?

as @Dino-at-Google mentioned.

there is no need to habe 2 consecutive policies just to substring a value....

I did try adding '<IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables>' to the policy. This didn't work. When I try to substring in :

<Headers> <Header name="x-app-name">{substring(dev.app-name,0,4)}</Header> </Headers>

The above doesn't work.

When I substring using the policy above, than access the variable in a seperate policy as show below it works:

<Headers> <Header name="x-app-name">{assigned}</Header> </Headers>

Not sure if this is because I am using the function in the header tag, but there is something in the policy which is causing it to fail.

Found this Q&A when looking for an error in my code:

substring(var, 0, 64)

- and figured the spaces were causing the substring function to fail, and it works like so:

substring(var,0,64)

That is, although the docs explicitly state their examples with spaces.

Adding this hint here just in case someone stumbles in here for the same reason I did - hope that's OK.

I ran into a similar issue where substring was unexpectedly returning empty string. Setting IgnoreUnresolvedVariables AND removing the space after the comma resolved it.

 

<Template>{substring(request.header.authorization,7)}</Template>

 

Removing only space without IgnoreUnresolvedVariables resulted in "Unresolved variable : request.header.authorization,7"

Having only IgnoreUnresolvedVariables also resulted in empty string.

Very vexing behavior. It appears the examples in the Apigee Edge docs were fixed, but the GCP docs were not.

Thanks for highlighting this, as it's a small but important detail. Have raised a documentation bug against this.