redirect - 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>
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.
解决方案
好的,如果你确定 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>
使用variablePrefix和variableSuffix来引用您在 javascript 策略中分配的值,在这种情况下,我指的是“serviceCalloutRequest”。
推荐阅读
- node.js - 用 Jest 在 Express 控制器中模拟 fs.existsSync
- java - setValue 里面的 onDataChange
- kotlin - 实体和 POJO 必须在 Kotlin 中具有可用的公共构造函数
- events - 组合框有问题,它没有加载de选项(pyqt5)
- javascript - 如何在 Python 中生成带有徽标的二维码
- python - 为什么我在每个方法调用上都传递一个参数?
- javascript - 如何在提交表单后阻止 React App 重置所有内容
- jenkins-pipeline - Jenkins DSL Pipeline 作业中的重试和超时
- variables - wxPython使用变量来引用对象
- mysql - 禁用 ONLY_FULL_GROUP_BY mysql 8 Ubuntu 20.04 LTS