从aws Lambda函数向REST API发送JSON POST请求

时间:2020-08-10 02:40:41

标签: javascript react-native rest aws-lambda

好吧,我试图通过将aws Lambda函数的发布请求发送到我的REST API来创建新的神奇宝贝:

var aws = require("aws-sdk");

// define our target API as a "service
const svc = new aws.Service({
  // the API base URL
  endpoint: 'http://189.159.115.72/api/pokemons',

  // don't parse API responses
  // (this is optional if you want to define shapes of all your endpoint responses)
  convertResponseTypes: false,

  // and now, our API endpoints
  apiConfig: {
    metadata: {
      protocol: "rest-json", // we assume our API is JSON-based
    },
    operations: {
      // get a record by id
      // example how to send JSON data in the request body
      createPokemon: {
        http: {
          method: "POST",
        },
        input: {
          type: "structure",
          required: ["data"],
          // use "data" input for the request payload
          payload: "data",
          members: {
            data: {
              type: "structure",
              required: ["name"],
              // the shape of the body object
              members: {
                name: {},
              },
            },
          },
        },
      },
    },
  },
});

svc.isGlobalEndpoint = true;

exports.handler = async (event, context, callback) => {
  console.log(event);

  svc.createPokemon(
    {
      data: {
        name: "Blastoise",
      },
    },
    (err, data) => {
      if (err) {
        console.error(">>> operation error:", err);
        return callback(err);
      }

      console.log("new record:", data);

      callback();
    }
  );
};

我无法发布请求...我的MYSQL数据库未注册新的宠物小精灵。 但是我可以从https://apitester.com/进行操作,因此我的REST API可以正常工作。

唯一标记的错误是在我的Android仿真器上:Invalid Lambda function output : Invalid JSON

enter image description here

有人知道如何解决此问题吗?预先感谢。

编辑:

来自https://apitester.com/的控制台日志

enter image description here

enter image description here

然后在aws网站上登录控制台: enter image description here

编辑#2 :(添加了响应正文屏幕截图)

enter image description here

0 个答案:

没有答案