【问题标题】:Apigee - how to redirect to a different resourceApigee - 如何重定向到其他资源
【发布时间】:2013-12-28 02:29:42
【问题描述】:

我有一个使用 Apigee 创建的 API 代理,它可以正常工作。但是,我想更改资源名称。

目前,我有以下资源:

<proxy>/collection/{Id}/products 重定向到<myService>/collection/{Id}/products

我想像这样重命名代理资源:

<proxy>/collection/{Id}/apps 重定向到<myService>/collection/{Id}/products

使用 Apigee 实现这一目标的最佳方法是什么?

干杯, 查夫达尔

【问题讨论】:

  • 你现有的 xml 是什么?这是在哪个端点(代理或目标)上?
  • 请澄清您所说的“重定向”是什么意思。您想发回 Location 标头,还是尝试代理到目标上的其他路径?
  • 我正在尝试代理到不同的目标。谢谢!

标签: proxy apigee


【解决方案1】:

如果您想将 /collection/{id}/apps 的请求代理到 /collection/{id}/products,请使用以下 sn-p 代码向目标请求添加 JavaScript 策略:

var collection_id = 10; //you should get this using context.getVariable()
var path = '/collection/' + collection_id + '/products'; //new path
var current_target_url = context.getVariable('target.url'); //get the existing target url
context.setVariable('target.copy.pathsuffix', false); //tell apigee to not copy the path over to the target
context.setVariable('debug.js.path', current_target_url + path); //this line is only for seeing the value in the trace tool
context.setVariable('target.url', current_target_url + path); //set the new path

【讨论】:

  • 试过这个。看起来像是一种工作方法。唯一的问题是,这会将所有资源重定向到产品,而不仅仅是应用程序。我尝试在代理请求中执行此操作: var collection_id = 1; // 你应该使用 context.getVariable() 得到这个 var path = '/collection/' + collection_id + '/products'; //新路径 var current_request_path = context.getVariable('request.path'); //获取现有请求路径 context.setVariable('request.path', path); // 设置新路径 这不起作用.. 有什么想法吗?
  • 您需要在目标上设置新的 URL,因为这是请求的目的地。您可以使用条件包装代码:if(context.getVariable('proxy.pathsuffix') == "/collection/{id}/apps"){...}。或者,将相同的逻辑添加到 JavaScript <Step><Condition>
猜你喜欢
  • 2021-06-07
  • 1970-01-01
  • 2010-11-09
  • 2021-04-09
  • 2019-02-16
  • 2017-03-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多