首页 > 解决方案 > 如何使用 AWS lambda 函数获取 URL 参数?

问题描述

我正在为 API 使用 Netlify 函数,除了我需要访问 URL 参数时,其中大部分都可以正常工作

这是我必须获取参数的片段:

func Handler(ctx context.Context, request events.APIGatewayProxyRequest) (Response, error) {

    id := request.PathParameters["id"]

    ...
}

func main() {
    lambda.Start(Handler)
}

我有其他功能正常工作,不需要 URL 参数,但无法弄清楚如何让这些功能正常工作,我尝试了多种不同的选择:

https://example.com/endpoint/1
https://example.com/endpoint/id=1
https://example.com/endpoint?id=1

以上均未在命中端点时返回 id 路径参数

标签: goaws-lambdaaws-lambda-go

解决方案


您可以使用request.QueryStringParameters["id"]从查询参数中获取 id

func Handler(ctx context.Context, request events.APIGatewayProxyRequest) (Response, error) {

    id := request.QueryStringParameters["id"]

    ...
}

并打电话给https://example.com/endpoint?id=1


推荐阅读