Как добавить данные в формате JSON в запросе http в AWS lambda? - PullRequest
0 голосов
/ 02 мая 2019
const http = require('http');

exports.handler = function(event, context) {
   var url = event && event.url;
    http.post(url, function(res) {
    context.succeed();
  }).on('error', function(e) {
    context.done(null, 'FAILURE');
  });

};

Я использую это в своем лямбда-коде AWS для отправки http-запроса.

У меня есть файл JSON, который необходимо отправить как часть этого почтового запроса.Как добавить JSON?Где мне указать?

1 Ответ

0 голосов
/ 02 мая 2019

Если вы спрашиваете, откуда взять эту форму файла json, то s3 будет правильным местом для размещения этого файла, чтения лямбда-выражения и создания сообщения.

const obj= {'msg': [
{
  "firstName": "test",
  "lastName": "user1"
},
{
  "firstName": "test",
  "lastName": "user2"
}
]};

request.post({
  url: 'your website.com',
  body: obj,
  json: true
}, function(error, response, body){
console.log(body);
});

С просто http

const postData = querystring.stringify({
  'msg': 'Hello World!'
});

const options = {
  hostname: 'www.google.com',
  port: 80,
  path: '/upload',
  method: 'POST',
  headers: {
    'Content-Type': 'application/x-www-form-urlencoded',
    'Content-Length': Buffer.byteLength(postData)
  }
};

const req = http.request(options, (res) => {
  console.log(`STATUS: ${res.statusCode}`);
  console.log(`HEADERS: ${JSON.stringify(res.headers)}`);
  res.setEncoding('utf8');
  res.on('data', (chunk) => {
    console.log(`BODY: ${chunk}`);
  });
  res.on('end', () => {
    console.log('No more data in response.');
  });
});

req.on('error', (e) => {
  console.error(`problem with request: ${e.message}`);
});

// Write data to request body
req.write(postData);
req.end();
...