setup https code on AWS lambda use NodeJS

Viewed 49

i need a help. i can't store value to database when i build Http code on lambda.

I has setup AWS lambda function that build with SQS-poller. so i build the code on index.js file on lambda with this

const http = require('http');
var fs = require('fs');

 exports.handler = async (event) => { 
    let postData = JSON.stringify(event);

    var options = {
      'method': 'POST',
      'hostname': 'mylink.com',
      'path': '/index.php?route=report',
      'headers': {
        'Content-Type': 'application/json'
      }
    };
    
    var req = http.request(options, function (res) {
      var chunks = [];
    
      res.on("data", function (chunk) {
        chunks.push(chunk);
      });
    
      res.on("end", function (chunk) {
        var body = Buffer.concat(chunks);
        console.log(body.toString());
      });
    
      res.on("error", function (error) {
        console.error(error);
      });
    });
    
    req.write(postData);
    
    req.end();
    console.log(postData);
};

value for let postData = JSON.stringify(event); is this

{
    "Records": [
        {
            "messageId": "1123",
            "receiptHandle": "MessageReceiptHandle",
            "body": "Hello from SQS!", 
            .......
        }
    ]
}

i just want to store this "body": "Hello from SQS!",

so in my end point url http://mylink.com/index.php?route=report i build a simple code like this

    public function report(){ 
        $dataJson = file_get_contents('php://input');
        $decode = json_decode($dataJson, true); 
        $this->db->query("INSERT INTO report_data (report_id, text, date_added) VALUES (NULL, '".$decode['Records'][0]['body']."', current_timestamp())");
    }

but the value from "body": "Hello from SQS!", is not store to my database. it can create a new row on table report_data with empty text.

i already invetigate and no found any error.

please help me. i need to store value Hello from SQS! to table report_data

0 Answers
Related