UnresolvedVariable issue in simple JSON at APIGEE

I am receiving the following error:

curl -i -k -X POST "https://eval.example.com/translate/v1?lang=de" -H "Content-Type:application/json" -d '{ "text": "Hello world!" }'
HTTP/2 500
content-type: application/json
x-request-id: aec401f7-8e23-4768-b6fb-d02c5a4152a6
content-length: 121
date: Thu, 18 May 2023 21:41:46 GMT
via: 1.1 google
alt-svc: h3=":443"; ma=2592000,h3-29=":443"; ma=2592000

{"fault":{"faultstring":"Unresolved variable : $.text,JsonRequest","detail":{"errorcode":"entities.UnresolvedVariable"}}}

For following script:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<AssignMessage continueOnError="false" enabled="true" name="AM-BuildTranslateRequest">
<AssignVariable>
<Name>language</Name>
<Template>{firstnonnull(request.queryparam.lang,propertyset.language.output)}</Template>
</AssignVariable>
<AssignVariable>
<Name>JsonRequest</Name>
<Ref>request.content</Ref>
</AssignVariable>
<AssignVariable>
<Name>text</Name>
<Template>{jsonPath($.text,JsonRequest)}</Template>
</AssignVariable>
<Set>
<Payload contentType="application/json">{"q":"{text}","target":"{language}"}</Payload>
<Verb>POST</Verb>
</Set>
<AssignTo createNew="false" transport="http" type="request"/>
</AssignMessage>

 

Not sure what went wrong and how to resolve this. Tried many possibilities but since it is part of a quest, we have to follow the below instructions and should create accordingly.

  1. An AssignMessage policy named AM-BuildTranslateRequest should be used to create the backend request used in the translate conditional flow.
  • Use an AssignVariable with a template to create variables which will be used later in a logged message. The variable named text should use the jsonPath message template function to extract the text field from the request.

  • The variable named language should be created by using the firstnonnull message template function. This variable should contain the lang query parameter value if it exists, and the language property set's output property for the target language if the lang query parameter has not been specified.

  • A Set section should be used to set the JSON payload required by the backend service. Both variables you have created will be used in the payload.

0 3 213
3 REPLIES 3

Try to replace this

<AssignVariable>
  <Name>text</Name>
  <Template>{jsonPath($.text,JsonRequest)}</Template>
</AssignVariable>

..with this:

<AssignVariable>
  <Name>path1</Name>
  <Value>$.text</Value>
</AssignVariable>
<AssignVariable>
  <Name>text</Name>
  <Template>{jsonPath(path1,JsonRequest)}</Template>
</AssignVariable>

Thanks, tried and it worked as by giving expected request and response payload BUT the quest tracker is not satisfied with the code and it says "Please create the 'AM-BuildTranslateRequest' AssignMessage policy with the correct configuration and redeploy the API proxy." I was like "Come on!...."., Any idea why the quest tracker is giving this response instead of giving full marks.

The code in the respective same location:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<AssignMessage continueOnError="false" enabled="true" name="AM-BuildTranslateRequest">
<AssignVariable>
<Name>language</Name>
<Template>{firstnonnull(request.queryparam.lang,propertyset.language.output)}</Template>
</AssignVariable>
<AssignVariable>
<Name>jsonRequest</Name>
<Ref>request.content</Ref>
</AssignVariable>
<AssignVariable>
<Name>path1</Name>
<Value>$.text</Value>
</AssignVariable>
<AssignVariable>
<Name>text</Name>
<Template>{jsonPath(path1,jsonRequest)}</Template>
</AssignVariable>
<Set>
<Payload contentType="application/json">{"q":"{text}","target":"{language}"}</Payload>
</Set>
<AssignTo createNew="false" transport="http" type="request"/>
</AssignMessage>

It worked but it is not accepted as in the screenshot.Error On Quest.PNG

This issue is resolved somehow by adding <IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables>

The script that worked is :

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<AssignMessage continueOnError="false" enabled="true" name="AM-BuildTranslateRequest">
<DisplayName>AM-BuildTranslateRequest</DisplayName>
<AssignVariable>
<Name>text</Name>
<Template>{jsonPath($.text,request.content)}</Template>
</AssignVariable>
<AssignVariable>
<Name>language</Name>
<Template>{firstnonnull(request.queryparam.lang,propertyset.language.output)}</Template>
</AssignVariable>
<IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables>
<AssignTo createNew="false" transport="http" type="request"/>
<Set>
<Payload contentType="application/json">{"q":"{text}","target":"{language}"}</Payload>
</Set>
</AssignMessage>

If anyone can explain how <IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables> did this magic, that would be helpful.