replace response.content in AssignMessage Policy

Hi All,

I have read the documentation on replacing strings in the response content. Seems there are two ways: via AssignMessage Policy or via a Javascript Policy. Is there any performance difference between these methods?

Also I am wondering why my policy doesn't work.

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<AssignMessage name="AM-Set-Custom-Response">
    <AssignTo createNew="false" type="response"/>
    <IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables>
    <Set>
        <Payload>{replace(response.content, '^https://urltoreplace/','https://replacementurl')}</Payload>
    </Set>
</AssignMessage>   

Thanks again for all the support this community provides.

0 1 395
1 REPLY 1

I don't have an estimate for performance differences. AssignMessage is generally very fast, and JavaScript is generally slower. But ... I would suggest that you just use the simplest thing possible. Optimize on whatever seems to be most maintainable. Worry about performance later.

as for your policy, I suggest 3 things:

  • eliminate spaces in the template
  • use temp variables.
  • use replaceAll

Like this:

<AssignMessage name="AM-Set-Custom-Response">
  <IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables>
  <AssignVariable>
    <Name>needle</Name>
    <Value>^https://urltoreplace/</Value>
  </AssignVariable>
  <AssignVariable>
    <Name>replacement</Name>
    <Value>https://replacementurl</Value>
  </AssignVariable>

  <Set>
      <Payload>{replaceAll(response.content,needle,replacement)}</Payload>
  </Set>
</AssignMessage>   

Also, last thing, this is ineffectual, a no-op:

<AssignTo createNew="false" type="response"/>

...so you can safely omit it from your policy.