You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过Lambda Function Url获取查询参数?调用时Event为空

Lambda Function URL 获取查询参数问题解决

问题核心

使用Lambda Function URL时,处理函数的event为空,无法获取如id=123这类查询参数。

解决步骤

1. 修正代码写法

避免async函数与callback混用,Lambda推荐在异步函数中直接返回响应。以下是可正常获取查询参数的代码:

exports.handler = async (event) => {
  // 提取查询参数,兼容无参数场景
  const id = event.queryStringParameters?.id || '未传入id参数';
  
  return {
    statusCode: 200,
    body: JSON.stringify({
      收到的id: id
    }),
    headers: {
      "Access-Control-Allow-Origin": "*",
      "Content-Type": "application/json"
    }
  };
};

2. 排查配置问题

如果修正代码后event仍为空,检查Function URL的两项关键配置:

  • Invoke Mode:确保设置为BUFFERED(默认值),STREAM模式下event结构会发生变化,无法直接用queryStringParameters读取参数。
  • Payload Format Version:选择2.0或1.0均可,两种格式都支持查询参数读取,仅结构细节略有差异。

3. 了解event结构(Payload Format 2.0)

当通过https://functionalurl.aws?id=123发起GET请求时,event的核心结构如下:

{
  "version": "2.0",
  "rawQueryString": "id=123",
  "queryStringParameters": {
    "id": "123"
  },
  // 其他字段:headers、requestContext等
}

内容的提问来源于stack exchange,提问作者Gracie williams

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.19 10:30:44