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
<?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>
<?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! Go to Solution.
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>