How to apply assign message policy

I have been given a task, to apply an assigned message policy on translate proxy, as per the below requirement.

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 that 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.

  • The [AssignTo] element should use the existing request message.

    The AssignVariable sections in the AssignMessage policy should look similar to this:

    <AssignVariable>
      <Name>...</Name>
      <Template>...</Template>
    <AssignVariable>

My proxy file is as below 

 

 

 

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ProxyEndpoint name="default">
    <PreFlow name="PreFlow">
        <Request/>
        <Response/>
    </PreFlow>
    <Flows>
        <Flow name="translate">
            <Description/>
            <Request>
                <Step>
                    <Name>AM-BuildTranslateRequest</Name>
                </Step>
            </Request>
            <Response>
                <Step>
                    <Name>AM-BuildTranslateResponse</Name>
                </Step>
            </Response>
            <Condition>(proxy.pathsuffix MatchesPath "/") and (request.verb = "POST")</Condition>
        </Flow>
        <Flow name="getLanguages">
            <Description/>
            <Request>
                <Step>
                    <Name>AM-BuildLanguagesRequest</Name>
                </Step>
            </Request>
            <Response>
                <Step>
                    <Name>JS-BuildLanguagesResponse</Name>
                </Step>
            </Response>
            <Condition>(proxy.pathsuffix MatchesPath "/languages")</Condition>
        </Flow>
    </Flows>
    <PostFlow name="PostFlow">
        <Request/>
        <Response/>
    </PostFlow>
    <HTTPProxyConnection>
        <BasePath>/translate/v1</BasePath>
    </HTTPProxyConnection>
    <RouteRule name="default">
        <TargetEndpoint>default</TargetEndpoint>
    </RouteRule>
</ProxyEndpoint>

 

 

 

and my policy files are as below

  • AM-BuildTranslateRequest

 

 

 

<?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>
    <Set>
        <Payload contentType="application/json">
      {
       "q":"{text}",
       "target":"{language}"
      }
    </Payload>
        <Verb>POST</Verb>
    </Set>
    <AssignTo createNew="false" transport="http" type="request"/>
</AssignMessage>

 

 

 

 

  • AM-BuildTranslateResponse

 

 

 

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<AssignMessage continueOnError="false" enabled="true" name="AM-BuildTranslateResponse">
    <DisplayName>AM-BuildTranslateResponse</DisplayName>
    <AssignVariable>
        <Name>translated</Name>
        <Template>{jsonPath($.data.translations[0].translatedText,message.content)}</Template>
    </AssignVariable>
    <AssignVariable>
        <Name>affiliate </Name>
        <JSONPath>$.affiliate[*]</JSONPath>
    </AssignVariable>
    <Set>
        <Payload contentType="application/json">
      {"translated":"{translated}"}
    </Payload>
        <Verb>POST</Verb>
    </Set>
    <IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables>
    <AssignTo createNew="true" transport="http" type="response"/>
</AssignMessage>

 

 

 


Can anyone help me here like what has been not implemented as per the requirement (functionally It is working fine, but I want to implement it exactly how been asked in the requirement)?   

Solved Solved
0 24 3,177
1 ACCEPTED SOLUTION

Hi @satz and @dchiesa1 ,

Thank you so much for your help, but it was my bad that I forget to update here about the resolution of it, Yes space was definatly the problem, but there were some other issues as well. the final policy which worked for me is as below. 

 

 

 

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<AssignMessage continueOnError="false" enabled="true" name="AM-BuildTranslateRequest">
    <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>
    <Set>
        <Payload contentType="application/json">{"q":"{text}","target":"{language}"}</Payload>
        <Verb>POST</Verb>
    </Set>
    <AssignTo createNew="false"/>
</AssignMessage>

 

 

 

View solution in original post

24 REPLIES 24

    <AssignVariable>
        <Name>text </Name>
        <!-- NO NO NO -->
        <Template>{jsonPath(text,request.content)} </Template>
    </AssignVariable>

    <!-- YES -->
    <AssignVariable>
        <Name>jsonpath1</Name>
        <Value>$.whatever.etc</Value>
    </AssignVariable>
    <AssignVariable>
        <Name>text</Name>
        <Template>{jsonPath(jsonpath1,request.content)}</Template>
    </AssignVariable>

Hi @prabhat_shukla ,
Can you please provide the solution what all things we need to add in JS-BuildLanguagesResponse.js file .

Thanks advance .

If you add questions or comments in the middle of a thread that is resolved, it is unlikely that anyone will see it, and help you.  You need to ask a new question.  

Hi @dchiesa1 ,

here can use word 'text' at the place of 

$.whatever.etc

 

 

in the section provided

    <AssignVariable>
        <Name>jsonpath1</Name>
        <Value>$.whatever.etc</Value>
    </AssignVariable>

 

I suppose so. I would expect to see a jsonpath expression like $.text.

I have tried this, but still no luck, i am getting the same response, as shared below. 

prabhat_shukla_0-1651257728205.png

 

prabhat_shukla_1-1651257781078.png

refer this image

Ya, I think you are working through... I don't know... a tutorial or something.  I don't know what's required there, or what you need to accomplish. Maybe there is a way to reach out for help from within the tutorial or training. 

Thanks @dchiesa1 , 

I have reported it to the support team as well, as I can see the policy is now as been asked but still if any bits are missing so they can help,
anyways thanks for all your help and appreciable efforts 🙂 

The variable language in the AM-BuildTranslateRequest has some extra space in it.  I guess that triggers the error

good catch.  Is it truly there in the configuration or only an artifact of the copy/paste? In any case in the actual configuration the variable should be like this: 

 

   ...
   <AssignVariable>
        <Name>text</Name> <!-- no spaces between the elements -->
        <Template>{jsonPath(text,request.content)}</Template>
    </AssignVariable>
    <AssignVariable>
        <Name>language</Name> <!-- no spaces between the elements -->
        <Template>{firstnonnull(request.queryparam.lang,propertyset.language.output)}</Template>
    </AssignVariable>
    ...

 

Hi @satz and @dchiesa1 ,

Thank you so much for your help, but it was my bad that I forget to update here about the resolution of it, Yes space was definatly the problem, but there were some other issues as well. the final policy which worked for me is as below. 

 

 

 

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<AssignMessage continueOnError="false" enabled="true" name="AM-BuildTranslateRequest">
    <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>
    <Set>
        <Payload contentType="application/json">{"q":"{text}","target":"{language}"}</Payload>
        <Verb>POST</Verb>
    </Set>
    <AssignTo createNew="false"/>
</AssignMessage>

 

 

 

GREAT!  Glad you sorted it out.  And Thank you for taking the time to contribute your experience! It helps everyone else when people share.

Hi Prabhat,

Congrats for getting certified. I am also trying to give the Challenge lab and got stuck exactly where you were having issues. Please see my other post and see if you can help:

https://www.googlecloudcommunity.com/gc/Apigee/simple-jsonPath-expression-doesn-t-work/m-p/458875#M7...

What were the other issues you were facing other than the space issue ? I tried the same AssignMessage policy you have shared but that fails as well with "Unresolved variable $.text,request.content".

Thanks

Vaseem

Hi Did you get past the Quota step of this lab. It's gives me error with quota configs below -

<Quota name="Q-EnforceQuota" type="calendar">
  <DisplayName>Q-EnforceQuota</DisplayName>
  <StartTime>2022-07-01 12:00:00</StartTime>
  <UseQuotaConfigInAPIProduct stepName="VAK-VerifyKey">
    <DefaultConfig>
      <Allow
          countRef="verifyapikey.VAK-VerifyKey.apiproduct.developer.quota.limit">5</Allow>
      <Interval
          ref="verifyapikey.VAK-VerifyKey.apiproduct.developer.quota.interval">1</Interval>
      <TimeUnit
          ref="verifyapikey.VAK-VerifyKey.apiproduct.developer.quota.timeunit">hour</TimeUnit>
    </DefaultConfig>
  </UseQuotaConfigInAPIProduct>
  <Distributed>true</Distributed>
  <Synchronous>true</Synchronous>
</Quota>

thanks,

Aakash

Your question is completely new !

ask a new question, you’ll get better results. 

Hi @aakashsharmaa5
Yes, I have completed all the steps of this lab and been certified as well.
Prabhat's certificate 

Can you share the link of lab you are doing , Just checking if we are on same page.

 

I'm stuck with the task 2, with the AM-BuildLanguagesRequest and the javascript policy. Can you please help me with the other 2 policies needed in this task 2.

Ask a new question! (and be specific.  Most of us don't know what "task 2" is.) 

Hi,

I was asking @prabhat_shukla

It's regarding the same lab doubt that he posted here. 

 

Please ask a new question! Thanks.

thanks ! i am able to get past it and complete the lab my Quota policy wasn't configured properly. 

Hi @aakashsharmaa5 , @ashmitadas23 

Just saw your message, Let me know if you are still stuck 

thanks i was able to get past after raising it with qwicklabs support team.