Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/amazon-web-services/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Amazon web services 如何通过AWS HTTP API向Lambda发送参数_Amazon Web Services_Http_Aws Lambda_Aws Api Gateway - Fatal编程技术网

Amazon web services 如何通过AWS HTTP API向Lambda发送参数

Amazon web services 如何通过AWS HTTP API向Lambda发送参数,amazon-web-services,http,aws-lambda,aws-api-gateway,Amazon Web Services,Http,Aws Lambda,Aws Api Gateway,为了节省成本,我已经从RESTAPI切换到HTTP API。我是AWS的不速之客,所以请对我耐心点 LAMBDA 我有一个简单的Lambda函数,它返回与给定值相同的值。当从实际的Lambda控制台进行测试时,它工作良好 exports.handler = async (event) => { // TODO implement const response = { statusCode: 200, body: "hello&quo

为了节省成本,我已经从RESTAPI切换到HTTP API。我是AWS的不速之客,所以请对我耐心点

LAMBDA

我有一个简单的Lambda函数,它返回与给定值相同的值。当从实际的Lambda控制台进行测试时,它工作良好

exports.handler = async (event) => {
    // TODO implement
    const response = {
        statusCode: 200,
        body: "hello" + event["key1"]
    };
    return response;
};
使用JSON输入从Lambda控制台测试函数时: {“key1”:“value1”},它返回“hellovalue1”。 这就是我希望GET/POST请求返回的内容

API网关(HTTP API)

我创建了一个简单的HTTP API,它将导致上面的lambda函数。

当从浏览器调用上面的链接时,它返回helloundefined

我假设传递参数的方式是[上面的链接]?key1=value1,但它也返回helloundefined

当使用其他在线工具通过GET/POST请求向上面的链接发送JSON数据时,结果同样是,helloundefined

荣誉奖:通过邮递员发送请求时,会显示一个错误:

CORS错误:由于CORS策略,请求已被阻止

如何使用HTTP API将参数传递给AWS Lambda


提前感谢。

将取决于您的api网关配置,以正确地将查询参数、POST或PUT正文(注意:忽略GET请求正文)、表单数据映射到lambda的Json输入。仔细观察——注意怎么做

整个请求按原样通过表示任何HTTP方法的catch all ANY方法发送到后端Lambda函数。实际的HTTP方法由客户端在运行时指定。ANY方法允许您对所有受支持的HTTP方法使用单个API方法设置:DELETE、GET、HEAD、OPTIONS、PATCH、POST和PUT

后来他们展示了如何

curl -v -X POST "https://r275xc9bmd.execute-api.us-east-1.amazonaws.com/test/helloworld?name=John&city=Seattle" -H "content-type: application/json" -H "day: Thursday" -d "{ \"time\": \"evening\" }"
或者,对于GET请求

curl -X GET \
  'https://r275xc9bmd.execute-api.us-east-1.amazonaws.com/test/helloworld?name=John&city=Seattle' \
  -H 'content-type: application/json' \
  -H 'day: Thursday'
在此处提供的catch-all配置中捕获所有数据


但是,如果这种集成不完全正确,数据将无法正确传递,因此请仔细观察。

将取决于api网关配置,以正确地将查询参数、POST或PUT正文(注意忽略GET请求正文)、表单数据映射到lambda的Json输入。仔细观察——注意怎么做

整个请求按原样通过表示任何HTTP方法的catch all ANY方法发送到后端Lambda函数。实际的HTTP方法由客户端在运行时指定。ANY方法允许您对所有受支持的HTTP方法使用单个API方法设置:DELETE、GET、HEAD、OPTIONS、PATCH、POST和PUT

后来他们展示了如何

curl -v -X POST "https://r275xc9bmd.execute-api.us-east-1.amazonaws.com/test/helloworld?name=John&city=Seattle" -H "content-type: application/json" -H "day: Thursday" -d "{ \"time\": \"evening\" }"
或者,对于GET请求

curl -X GET \
  'https://r275xc9bmd.execute-api.us-east-1.amazonaws.com/test/helloworld?name=John&city=Seattle' \
  -H 'content-type: application/json' \
  -H 'day: Thursday'
在此处提供的catch-all配置中捕获所有数据


但是,如果这种集成不完全正确,数据将无法正确传递,因此请仔细观察。

我尝试使用HTTP API复制该问题。我注意到该活动有以下形式

{
  version: '2.0',
  routeKey: 'ANY /MS_APITest',
  rawPath: '/MS_APITest',
  rawQueryString: 'key1=value1',
  headers: {
    accept: '*/*',
    'content-length': '0',
    host: 'xxxxx.execute-api.us-east-1.amazonaws.com',
    'user-agent': 'curl/7.72.0',
    'x-amzn-trace-id': 'Root=1-5f5afd55-332693f425fbcc7a032809da',
    'x-forwarded-for': 'xxxxxxxxx',
    'x-forwarded-port': '443',
    'x-forwarded-proto': 'https'
  },
  queryStringParameters: { key1: 'value1' },
  requestContext: {
    accountId: '820872329501',
    apiId: 'sg5mhha5ic',
    domainName: 'xxxx.execute-api.us-east-1.amazonaws.com',
    domainPrefix: 'sg5mhha5ic',
    http: {
      method: 'GET',
      path: '/MS_APITest',
      protocol: 'HTTP/1.1',
      sourceIp: 'xxxxxx',
      userAgent: 'curl/7.72.0'
    },
    requestId: 'SryFYjtUIAMEV5w=',
    routeKey: 'ANY /MS_APITest',
    stage: '$default',
    time: '11/Sep/2020:04:30:13 +0000',
    timeEpoch: 1599798613551
  },
  isBase64Encoded: false
}
可以看出,
?key1=value1
作为

queryStringParameters: { key1: 'value1' },
因此,lambda函数应为:

exports.handler = async (event) => {
    // TODO implement
    console.log(event)
    const response = {
        statusCode: 200,
        body: "hello" + event['queryStringParameters']["key1"]
    };
    return response;
};
通过以下方式验证了它的有效性:

curl https://xxxx.execute-api.us-east-1.amazonaws.com/MS_APITest?key1=value1
这导致:

hellovalue1

我尝试使用HTTP API复制该问题。我注意到该活动有以下形式

{
  version: '2.0',
  routeKey: 'ANY /MS_APITest',
  rawPath: '/MS_APITest',
  rawQueryString: 'key1=value1',
  headers: {
    accept: '*/*',
    'content-length': '0',
    host: 'xxxxx.execute-api.us-east-1.amazonaws.com',
    'user-agent': 'curl/7.72.0',
    'x-amzn-trace-id': 'Root=1-5f5afd55-332693f425fbcc7a032809da',
    'x-forwarded-for': 'xxxxxxxxx',
    'x-forwarded-port': '443',
    'x-forwarded-proto': 'https'
  },
  queryStringParameters: { key1: 'value1' },
  requestContext: {
    accountId: '820872329501',
    apiId: 'sg5mhha5ic',
    domainName: 'xxxx.execute-api.us-east-1.amazonaws.com',
    domainPrefix: 'sg5mhha5ic',
    http: {
      method: 'GET',
      path: '/MS_APITest',
      protocol: 'HTTP/1.1',
      sourceIp: 'xxxxxx',
      userAgent: 'curl/7.72.0'
    },
    requestId: 'SryFYjtUIAMEV5w=',
    routeKey: 'ANY /MS_APITest',
    stage: '$default',
    time: '11/Sep/2020:04:30:13 +0000',
    timeEpoch: 1599798613551
  },
  isBase64Encoded: false
}
可以看出,
?key1=value1
作为

queryStringParameters: { key1: 'value1' },
因此,lambda函数应为:

exports.handler = async (event) => {
    // TODO implement
    console.log(event)
    const response = {
        statusCode: 200,
        body: "hello" + event['queryStringParameters']["key1"]
    };
    return response;
};
通过以下方式验证了它的有效性:

curl https://xxxx.execute-api.us-east-1.amazonaws.com/MS_APITest?key1=value1
这导致:

hellovalue1

嗨,我只是想知道。提供的链接是针对RESTAPI的。它是否也适用于HTTP API?似乎OP希望在API网关中使用带有HTTP API的Lambda,而不是REST API。没错,REST API有大量关于如何使用和配置它们的文档。关于HTTP API以及如何设置它们的文档很少甚至没有。我一直在搜索映射选项,但什么也没找到。这正是困扰我的地方。@ZipholdHTTPAPI非常新,而且非常有限。这就解释了它的价格较低以及缺乏资源和教程的原因:-(嗨,我只是想知道。提供的链接是为REST API提供的。它也适用于HTTP API吗?似乎OP希望在API网关中将Lambda与HTTP API一起使用,而不是REST API。没错,REST API有大量关于如何使用和配置它们的文档。关于HTTP API以及如何设置它们的文档几乎没有。我一直在搜索g用于映射选项,但什么也没有找到。这正是困扰我的地方。@Ziphold HTTP API非常新,而且非常有限。这解释了它的价格较低以及缺乏资源和教程的原因:-(这是一种魅力!你解决了问题,理应获得一枚金牌。你的回答帮助了我,达到了无法想象的程度。谢谢!这是一种魅力!你解决了问题,理应获得一枚金牌。你的回答帮助了我,达到了无法想象的程度。谢谢!