How to send parameters through AWS HTTP API to Lambda

别说谁变了你拦得住时间么 提交于 2021-01-29 07:17:15

问题


I have switched from the REST API to HTTP API for it's cost efficiency. I am a noob to AWS so please be patient with me.

LAMBDA

I have a simple Lambda function that returns the same value it's given. It works fine when tested from the actual Lambda console.

exports.handler = async (event) => {
    // TODO implement
    const response = {
        statusCode: 200,
        body: "hello" + event["key1"]
    };
    return response;
};

When the function is tested from the Lambda console with JSON input: { "key1":"value1" }, it returns "hellovalue1". This is what I want my GET/POST request to return.

API GATEWAY (HTTP API)

I created a simple HTTP API which leads to the lambda function above. https://39lzilxqm2.execute-api.eu-central-1.amazonaws.com/MS_APITest

When the link above is called from the browser, it returns helloundefined.

I would assume the way of passing an argument would be [THE LINK ABOVE]?key1=value1, but that also returns helloundefined

When using other online tools to send JSON data to the link above through GET/POST requests, the result is, again, helloundefined

Honorable mention: When sending a request through postman, it displays an error:

CORS Error: The request has been blocked because of the CORS policy

How do I pass an argument to AWS Lambda using HTTP API?

Thank you in advance.


回答1:


I tried to replicate the issue using HTTP API. I noticed that the event has the following form.

{
  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
}

As can be seen the ?key1=value1 is passed as

queryStringParameters: { key1: 'value1' },

Therefore, the lambda function should be:

exports.handler = async (event) => {
    // TODO implement
    console.log(event)
    const response = {
        statusCode: 200,
        body: "hello" + event['queryStringParameters']["key1"]
    };
    return response;
};

I verified that it works using:

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

which resulted in:

hellovalue1



回答2:


it would be up to your api gateway configuration to properly map something - query parameters, POST or PUT body (note GET request bodies are disregarded), form data - to Json input for your lambda. Look closely at https://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-create-api-as-simple-proxy-for-lambda.html - note how

the entire request is sent to the backend Lambda function as-is, via a catch-all ANY method that represents any HTTP method. The actual HTTP method is specified by the client at run time. The ANY method allows you to use a single API method setup for all of the supported HTTP methods: DELETE, GET, HEAD, OPTIONS, PATCH, POST, and PUT.

Later they show how

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\" }"

or, for a GET request,

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'

all are captured in the catch-all configuration provided there.

However, if that integration isn't quite right, the data won't be passed properly, so look closely.



来源:https://stackoverflow.com/questions/63839474/how-to-send-parameters-through-aws-http-api-to-lambda

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!