0
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

GASからAWS API Gateway(Lambda)へリクエストするときのパラメーターの送受信方法

Last updated at Posted at 2020-08-16

#要点
・GASでpostする時は 'contentType': 'application/json' を指定してpayloadのjsonをstringifyしておく
・AWS側でparse

#コード
GoogleAppsScript

uploadToAws.gas
function uploadToAws() {
  const aws_url = "APIのURL"
  const params = {
    'method' : 'post', //get or post
    'contentType': 'application/json',
    'payload' : JSON.stringify({"data1": "hogehoge" })
  };
  const req = UrlFetchApp.fetch(aws_url, params);
  Logger.log(req.getContentText());
}

Lambda(Node.js)

lambdaSample.js
const aws = require('aws-sdk');

exports.handler = async (event) => {
    const data = (event.requestContext.http.method === "GET") ? event.queryStringParameters 
    : (event.requestContext.http.method === "POST") ? JSON.parse(event.body)
    : null;
    const response = {
        statusCode: (data) ? 200 : 503,
        body: JSON.stringify((data) ? data.data1 : "no_data"),
    };
    return response;
};
0
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?