【问题标题】:How do I send data from 1 api to another api using apigee如何使用 apigee 将数据从 1 个 api 发送到另一个 api
【发布时间】:2019-06-17 20:14:27
【问题描述】:

我最近开始使用 apigee 代理。请不要阻止我。 我正在使用邮递员将数据发布到代理中。 我已经分配了一个提取策略来从 json 数据中提取一些值,例如 速度、纬度、经度等 然后我根据客户要求使用分配策略将速度转换为 gpsspeed 等。 之后,我使用 javascript 策略输出速度是否高于 > 50,然后是高或低。 我举一个数据的例子。 现在我想将结果数据转发到另一个 api。也许任何 apigee 为测试目的提供的东西。我想查看结果 api 中的数据。 我使用服务调用策略将数据发送到另一个 url。 我附上政策。

 <?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>

我在它之前附加了一个javascript策略。 我附上政策。

 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

谁能帮助我如何将数据转发到另一个 api?我的程序对吗?从java脚本策略中提取变量的程序对吗?请指导我。

【问题讨论】:

  • 您确定您的javascript策略中的对象“response.content”在您将值设置回它后不为空
  • 它不是空的..

标签: redirect apigee


【解决方案1】:

好的,如果你确定 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”。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-04-01
    • 1970-01-01
    • 2018-08-09
    • 2020-02-09
    • 2014-08-24
    • 2021-09-06
    • 2022-01-26
    相关资源
    最近更新 更多