Unable to read from resource file in Apigee AssignMessage policy

DRR
Bronze 2
Bronze 2

I am trying to read the contents of a JSON file within an APIProxy using the AssignMessage policy.
I am getting null when I am trying to print the variable after reading. Please find the AssignMessage policy and the JavaScript code to print the variable.

I have referred to below post.

https://www.googlecloudcommunity.com/gc/Apigee/How-to-retrieve-the-content-of-resource-files-defined...

Please help me to resolve this issue.

AssignMessage policy

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<AssignMessage async="false" continueOnError="false" enabled="true" name="AM-ReadSpec">
<DisplayName>AM-ReadSpec</DisplayName>
<Properties/>
<AssignVariable>
<Name>JsonContent</Name>
<ResourceURL>jsc://Service-API.json</ResourceURL>
</AssignVariable>
<IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables>
<AssignTo createNew="false" transport="http" type="request"/>
</AssignMessage>

 

JavaScript to print the values

var JsonContent1= context.getVariable('JsonContent');

var Content1 = JSON.parse(JsonContent1);

print(Content1 );

0 7 242
7 REPLIES 7

1. Use proxy debug/trace. You should see your assign message policy set the JsonContent variable with the contents of the file

2. Is your JsonContent variable ie your resource file containing valid json? If it's not valid json, your JSON.parse() will fail. Although this should also give you an exception

3. If you have valid JSON, then I expect you'll only see [object Object] output in trace anyway. Is this what you're expecting and where exactly do you see the null? Otherwise you should directly output JsonContent1

Hi,

Sorry for late response.

The AssignMessage is not reading the file and assigning to variable 'specsContent'. Hence the value is printed as null in output when tested with trace enabled. Below is the AssignMessage policy. The json file exists under resources in API proxy.

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<AssignMessage async="false" continueOnError="false" enabled="true" name="AM-ReadSpec">
<DisplayName>AM-ReadSpec</DisplayName>
<Properties/>
<AssignVariable>
<Name>specsContent</Name>
<ResourceURL>jsc://LookupService-API.json</ResourceURL>
</AssignVariable>
<IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables>
<AssignTo createNew="false" transport="http" type="request"/>
</AssignMessage>

Thanks.

What version of Apigee are you using, please?  The ResourceURL as a child of AssignVariable works in Apigee X and hybrid. First available in Hybrid v1.5.0. If you're using Apigee Edge or OPDK, it's not available. 

A working example is attached.  Example assignmessage policy

<AssignMessage name='AM-Assign-Variable-From-Resource-URL'>
   <AssignVariable>
     <Name>variable-to-set</Name>
     <ResourceURL>jsc://foobar.json</ResourceURL>
   </AssignVariable>
</AssignMessage>

Example resource file foobar.json

{
  "hello" : "world",
  "life" : {
    "meaning" : 42
  }
}

trace snapshot

assign-var-resource-url.jpg

DRR
Bronze 2
Bronze 2

Hi,

Thanks. We are using Apigee edge and that could be the reason it is not working. Are there any other ways to read the json file inside Api proxy in Apigee edge, other than assigning json to variable and read it in Javascript?

Regards

I don't know of a way, other than that. Good reason to migrate?

Thanks. We are planning to migrate to Apigee X.

DRR - regarding migration, are you connected to a tech resource who can help get you started? someone like an Apigee Customer Engineer (CE) or a Google Cloud Tech Account Manager (TAM) ? The Apigee engineering team has an assessment tool that they can run on organizations in  Edge Public Cloud, giving you a report on any compatibility issues that might arise with the migration.  For example,  dependencies on things that are available in Edge but not in X. 

You can do it yourself, but that assessment is a n ice place to start from.