首页 > 解决方案 > How do I send data from 1 api to another api using apigee

问题描述

I am recently started using apigee proxy.Please do not block me. I am using postman to post data into proxy. I have assigned a extract policy to extract some values from the json data like speed,latitude, longitude etc. Then I have used a assign policy to convert speed into gpsspeed etc as per client requirement. After that i use javascript policy to output if speed is higher > 50 then high or low. I am giving an example of the data. Now i want to forward the resultant data to another api.Maybe anything apigee offers for testing purpose.I want to see the data in the resultant api. I used service call out policy to send data to another url. I am attaching the policy.

 <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
 <ServiceCallout name="ServiceCallout-GeocodingRequest1">
 <DisplayName>Inline request message</DisplayName>
 <Request variable="callAnotherService">
    <Set>
        <Payload contentType="application/json">
          {response.content} // reading data from javascript policy
        </Payload>
    </Set>
</Request>
<Response>CalloutResponse</Response>
<Timeout>30000</Timeout>
<HTTPTargetConnection>
    <URL>http://httpbin.org/anything</URL>
</HTTPTargetConnection>

trace call I have attached a javascript policy before it. I am attaching the policy.

 var content = context.getVariable("response.content") //read the response
  content = JSON.parse(content); //parse into an object
  if (content.speed > 50) { //apply the condition
  content.speed = "high";
   }
  else {
  content.speed = "low";
  }
  context.setVariable("response.content", JSON.stringify(content)); //set it 
  back on the response

Can anyone help me how can i forward the data to another api? Is my procedure right?Is the procedure to extract variables from java script policy right?Please guide me.

标签: redirectapigee

解决方案


好的,如果你确定 setVarible 之后的值不为空,那么试试下面的代码。

注意:在您的 JavaScript 策略中,我在将值设置回它之后更改了变量名称(为了不混淆)。

var content = context.getVariable("response.content") //read the response
content = JSON.parse(content); //parse into an object
if(content.speed > 50){ 
    content.speed = "high";
}
else{
    content.speed = "low";
}
context.setVariable("serviceCalloutRequest", JSON.stringify(content)); //I have changed from "response.content" to "serviceCalloutRequest"

并在服务标注政策中尝试:

<Request clearPayload="true" variable="callAnotherService">
    <IgnoreUnresolvedVariables>false</IgnoreUnresolvedVariables>
    <Set>       
        <Payload variablePrefix="@" variableSuffix="#">@serviceCalloutRequest#</Payload>
        <Verb>POST</Verb>
    </Set>
</Request>

使用variablePrefixvariableSuffix来引用您在 javascript 策略中分配的值,在这种情况下,我指的是“serviceCalloutRequest”。


推荐阅读