How do I send data from 1 api to another api using apigee

青春壹個敷衍的年華 提交于 2019-12-13 02:49:31

问题


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>

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.


回答1:


Okay, if you are sure that value after setVarible is not empty, So try code below.

Note: In your JavaScript Policy I have changed the variable name after set the value back to it (for not confusing).

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"

And try this in Service Callout Policy:

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

Use variablePrefix and variableSuffix to reference the value that you have assign in your javascript policy, In this case I refer from "serviceCalloutRequest".



来源:https://stackoverflow.com/questions/54340796/how-do-i-send-data-from-1-api-to-another-api-using-apigee

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!