title: Issure receiving HTTP POST request in JSON and saving it to MySQL using REST API
body: <p>I'm pretty new to javascript but I have a REST API I'm running on a LAN network. I have a MySQL database I'm trying to save a HTTP POST body to in JSON format. My js code will print the data as "result" for me but will not save it when it is sent from Postman, however if I hard code a fake entry into it as "var result = {JSON format entries}" it will then be able to be saved to MySQL. Here is my code:</p>
  
  <pre><code>var http = require('http');
  var express = require('express');
  var bodyParser = require('body-parser');
  const { parse } = require('querystring');
  var mysql = require("mysql");
  var app = express();
  var port = process.env.port || 8080;
  
  var connection = mysql.createConnection({
    host     : 'localhost',
    user     : 'root',
    password : '*******',
    database : 'userInfo'
  });
  connection.connect(function(err) {
    if (err) throw err
    console.log('You are now connected with mysql database...')
  })
  
  app.post('/licence', function (req, res) {
      collectRequestData(req, result =&gt; {
        console.log(result);
        connection.query('INSERT INTO licence SET ?',result, function (error, 
  results, fields) {
      if (error) throw error;
      res.end(JSON.stringify(results));
        });
       res.end(`Parsed data belonging to ${result.fname}`);
      });
  });
  
  function collectRequestData(request, callback) {
      const FORM_URLENCODED = 'application/json';
      if(request.headers['content-type'] === FORM_URLENCODED) {
          let body = '';
          request.on('data', chunk =&gt; {
              body += chunk.toString();
          });
          request.on('end', () =&gt; {
              callback(parse(body));
          });
      }
      else {
          callback(null);
      }
  }
  </code></pre>
  
  <p>I'm pretty sure I don't need this line "res.end(<code>Parsed data belonging to ${result.fname}</code>);" but what I am trying to figure out here is how to save result to the licence table in mysql. This is what console.log(result) prints: { '{"hash":"TestHaaaash", "size":13, "difficulty":47, "time":null, "timestamp":null, "date":null}': '' } I think this should be slightly different but I'm not sure how it should be or how to change it</p>