【问题标题】:How to test a GET and POST call from inside of Lambda如何从 Lambda 内部测试 GET 和 POST 调用
【发布时间】:2020-06-22 02:06:51
【问题描述】:

我正在学习 Lambda,创建了一个 Cloud Watch Metrics,每分钟触发一次 Lambda。目前,我的 Lambda 函数正在正常执行。但我想从 Lambda 内部进行虚假的 GET 和 POST 调用。如下所示:

var https = require('https');
       exports.handler = (event, context, callback) => {
       var params = {
                    host: "example.com",
                    path: "/api/v1/yourmethod"

                    };

  var req = https.request(params, function(res) {
    let data = '';
    console.log('STATUS: ' + res.statusCode);
    res.setEncoding('utf8');
    res.on('data', function(chunk) {
        data += chunk;
    });
    res.on('end', function() {
        console.log("DONE");
        console.log(JSON.parse(data));
    });
  });
   req.end();
 };

问题 - 由于我没有在 AWS 中托管任何 GET 和 POST 端点/代码,我有什么方法可以测试来自 Lambda 内部的 GET 调用和 POST 调用是否工作正常? AWS 中是否有任何方法可以在 AWS 中组合一个假端点或其他东西,并出于测试目的对其进行 REST 调用?

【问题讨论】:

    标签: amazon-web-services aws-lambda


    【解决方案1】:

    您可以使用 JSONPlaceholder 的 API 来测试对外部 API 的调用,
    他们发布了用于测试和原型设计的假在线 REST API
    您可以测试调用不同的 http 方法调用,例如 GET, PUT, POST, DELETE

    https://jsonplaceholder.typicode.com/

    【讨论】:

    • 酷服务。不知道。谢谢。
    【解决方案2】:

    如果您使用 Lambda 代理集成,事件将包含许多有关请求的附加信息:https://docs.aws.amazon.com/apigateway/latest/developerguide/set-up-lambda-proxy-integrations.html

    {
      "message": "Hello me!",
      "input": {
        "resource": "/{proxy+}",
        "path": "/hello/world",
        "httpMethod": "POST",
        "headers": {
          "Accept": "*/*",
          "Accept-Encoding": "gzip, deflate",
          "cache-control": "no-cache",
          "CloudFront-Forwarded-Proto": "https",
          "CloudFront-Is-Desktop-Viewer": "true",
          "CloudFront-Is-Mobile-Viewer": "false",
          "CloudFront-Is-SmartTV-Viewer": "false",
          "CloudFront-Is-Tablet-Viewer": "false",
          "CloudFront-Viewer-Country": "US",
          "Content-Type": "application/json",
          "headerName": "headerValue",
          "Host": "gy415nuibc.execute-api.us-east-1.amazonaws.com",
          "Postman-Token": "9f583ef0-ed83-4a38-aef3-eb9ce3f7a57f",
          "User-Agent": "PostmanRuntime/2.4.5",
          "Via": "1.1 d98420743a69852491bbdea73f7680bd.cloudfront.net (CloudFront)",
          "X-Amz-Cf-Id": "pn-PWIJc6thYnZm5P0NMgOUglL1DYtl0gdeJky8tqsg8iS_sgsKD1A==",
          "X-Forwarded-For": "54.240.196.186, 54.182.214.83",
          "X-Forwarded-Port": "443",
          "X-Forwarded-Proto": "https"
        },
        "multiValueHeaders":{
          'Accept':[
            "*/*"
          ],
          'Accept-Encoding':[
            "gzip, deflate"
          ],
          'cache-control':[
            "no-cache"
          ],
          'CloudFront-Forwarded-Proto':[
            "https"
          ],
          'CloudFront-Is-Desktop-Viewer':[
            "true"
          ],
          'CloudFront-Is-Mobile-Viewer':[
            "false"
          ],
          'CloudFront-Is-SmartTV-Viewer':[
            "false"
          ],
          'CloudFront-Is-Tablet-Viewer':[
            "false"
          ],
          'CloudFront-Viewer-Country':[
            "US"
          ],
          '':[
            ""
          ],
          'Content-Type':[
            "application/json"
          ],
          'headerName':[
            "headerValue"
          ],
          'Host':[
            "gy415nuibc.execute-api.us-east-1.amazonaws.com"
          ],
          'Postman-Token':[
            "9f583ef0-ed83-4a38-aef3-eb9ce3f7a57f"
          ],
          'User-Agent':[
            "PostmanRuntime/2.4.5"
          ],
          'Via':[
            "1.1 d98420743a69852491bbdea73f7680bd.cloudfront.net (CloudFront)"
          ],
          'X-Amz-Cf-Id':[
            "pn-PWIJc6thYnZm5P0NMgOUglL1DYtl0gdeJky8tqsg8iS_sgsKD1A=="
          ],
          'X-Forwarded-For':[
            "54.240.196.186, 54.182.214.83"
          ],
          'X-Forwarded-Port':[
            "443"
          ],
          'X-Forwarded-Proto':[
            "https"
          ]
        },
        "queryStringParameters": {
          "name": "me",
          "multivalueName": "me"
        },
        "multiValueQueryStringParameters":{
          "name":[
            "me"
          ],
          "multivalueName":[
            "you",
            "me"
          ]
        },
        "pathParameters": {
          "proxy": "hello/world"
        },
        "stageVariables": {
          "stageVariableName": "stageVariableValue"
        },
        "requestContext": {
          "accountId": "12345678912",
          "resourceId": "roq9wj",
          "stage": "testStage",
          "requestId": "deef4878-7910-11e6-8f14-25afc3e9ae33",
          "identity": {
            "cognitoIdentityPoolId": null,
            "accountId": null,
            "cognitoIdentityId": null,
            "caller": null,
            "apiKey": null,
            "sourceIp": "192.168.196.186",
            "cognitoAuthenticationType": null,
            "cognitoAuthenticationProvider": null,
            "userArn": null,
            "userAgent": "PostmanRuntime/2.4.5",
            "user": null
          },
          "resourcePath": "/{proxy+}",
          "httpMethod": "POST",
          "apiId": "gy415nuibc"
        },
        "body": "{\r\n\t\"a\": 1\r\n}",
        "isBase64Encoded": false
      }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-23
      • 2014-08-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多