重写api网关集成请求中的目标路径

问题描述 投票:0回答:2

假设我在 API Gateway 中有一个类似 /foo/{bar} 的资源。我想通过集成请求模板将请求路径转换为 /bing/baz/{bar}。

通过以下方式将“bar”设置到请求正文中是直接的:

{ "bar": "$inputs.params('bar')" }

如何在请求时重写目标路径?

此处的“示例请求响应”中暗示了解决方案:

https://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-mapping-template-reference.html

但是文档没有准确概述“使用输入模板:”的功能。

Resource: /things/{id}

With input template:
{
    "id" : "$input.params('id')",
    "count" : "$input.path(‘$.things').size()",
    "things" : $input.json(‘$.things')
}
amazon-web-services aws-api-gateway
2个回答
6
投票

您可能正在寻找映射模板变量“$context.resourcePath”,它将为您提供发出请求的资源路径。

编辑:

您可以在 HTTP 集成中的 URI 字段中使用路径参数,这允许您动态地将正文中的参数或字段映射到目标路径。语法与资源相同,因此参数周围有大括号,如“http://myapi.com/foo/bar/{baz}”。

然后您将能够为“baz”指定映射表达式。


0
投票

如果有人只想更改端点的前缀并保留路径的其余部分不变,例如

/v2/users
->
/api/users
这是一个解决方案。

创建类似

/v2/{my_path+}
的路线,并在集成中只需添加具有值
overwrite:path
的映射
/api/$request.path.my_path
。它将影响所有以
/v2/
开头的请求,因此可以节省大量手动工作。

文档:https://docs.aws.amazon.com/apigateway/latest/developerguide/http-api-parameter-mapping.html

© www.soinside.com 2019 - 2024. All rights reserved.