Conditional routing is happening, but final response is not populating using .js

Hi ,

Requirement:I have created a proxy with no endpoint(Proxy endpoint) . Created two conditional flows and call backend based on condition.If response comes from backend, I need to populate using java script.

Right now the condition is successfully evaluated and the backend service is called using a ServiceCallout policy.

But the .js written to display final response is throwing this error: ‘Execution of JavaScript-1 failed with error: Javascript runtime error: “TypeError: Cannot set property “content” of undefined to “{“one”:“two”,“key”:“value”}”. (JavaScript-1.js:2)”’.I can see the response from servicecall out ,but not able to display it as final response

Here is the code details:

ProxyEndpoint:

<ProxyEndpoint name="default"> 
  <Description/> 
  <FaultRules/> 
  <PreFlow name="PreFlow"> 
    <Request/> 
    <Response/> 
  </PreFlow> 
  <PostFlow name="PostFlow"> 
    <Request/> 
    <Response/> 
  </PostFlow> 
  <Flows> 
    <Flow name="Flow-1"> 
      <Description/> 
      <Request> 
        <Step> 
          <Name>Service-Callout-1</Name> 
        </Step> 
        <Step> 
          <Name>Extract-Variables-1</Name> 
        </Step> 
        <Step> 
          <Name>JavaScript-1</Name> 
        </Step> 
      </Request> 
      <Response/> 
      <Condition>(proxy.pathsuffix MatchesPath "/First")</Condition> 
    </Flow> 
    <Flow name="Flow-2"> 
      <Description/> 
      <Request> 
        <Step> 
          <Name>Service-Callout-2</Name> 
        </Step>                 
        <Step> 
          <Name>JavaScript-2</Name> 
        </Step> 
      </Request> 
      <Response/> 
      <Condition>(proxy.pathsuffix MatchesPath "/Second")</Condition> 
    </Flow> 
  </Flows> 
  <HTTPProxyConnection> 
    <BasePath>/conditionalroutingwithstaticendpoint</BasePath> 
    <Properties/> 
    <VirtualHost>default</VirtualHost> 
    <VirtualHost>secure</VirtualHost> 
  </HTTPProxyConnection> 
  <RouteRule name="noroute"/> 
</ProxyEndpoint>

Service-Callout-1 :

<ServiceCallout async="false" continueOnError="false" enabled="true" name="Service-Callout-1"> 
  <DisplayName>Service Callout-1</DisplayName> 
  <Properties/> 
  <Request clearPayload="true" variable="myRequest"> 
    <IgnoreUnresolvedVariables>false</IgnoreUnresolvedVariables> 
  </Request> 
  <Response>calloutResponse</Response> 
  <HTTPTargetConnection> 
    <Properties/> 
    <URL>http://echo.jsontest.com/key/value/one/two</URL> 
  </HTTPTargetConnection> 
</ServiceCallout>

JavaScript-1 :

var originalResponse = JSON.parse(context.getVariable('calloutResponse.content'));
context.proxyResponse.content = JSON.stringify(originalResponse);

Please help me on javascript part .

1 Like

Hi @veeraraghavendra,

Since you are defining the javascript policy in the request flow, context.proxyResponse is undefined.

Try adding the javascript policy in the response flow. (works for me, cross-verified)

1 Like

Kindly use the code tab to populate the code snippet. It eases readability.

Many thanks ,I will try and let you know.

Keep us posted, thank you

Many thanks again ,It is working for me now.Thanks a lot