如何使用 apigee 将数据从 1 个 api 发送到另一个 api [英] How do I send data from 1 api to another api using apigee

查看:39
本文介绍了如何使用 apigee 将数据从 1 个 api 发送到另一个 api的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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>

我在它之前附加了一个 javascript 政策.我正在附上政策.

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

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

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 之后的值不为空,那么试试下面的代码.

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

注意:在您的 JavaScript 政策中,我在将值重新设置后更改了变量名称(以免混淆).

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>

使用 variablePrefixvariableSuffix 来引用您在 javascript 策略中分配的值,在这种情况下,我从serviceCalloutRequest"中引用.

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

这篇关于如何使用 apigee 将数据从 1 个 api 发送到另一个 api的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆