ERROR while calling post request from rest api made with Node Js and express .the request data goes empty

Sherif Adel

Dears I used node js to build rest API and in post API I think request goes empty when I tested it from postman or soap UI but when using test rest functionality from web storm IDE it works fine and inserts records in the database. I don't know what is the problem exactly but it seems the data inserted not passed to the API

app.js

var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var mysql =require('mysql');
var index = require('./routes/index');
var users = require('./routes/users');
var connection  = require('express-myconnection');
var app = express();

// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');

// uncomment after placing your favicon in /public
//app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
//app.use(cookieParser());
//app.use(express.static(path.join(__dirname, 'public')));
app.use(function(req, res, next) {
    res.header("Access-Control-Allow-Origin", "*");
    res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
    next();
});

index.js

var express = require('express');
var router = express.Router();
var bodyParser = require('body-parser');

/*GET home page. */
router.get('/', function(req, res, next) {
  res.render('index', { title: 'Express' });
});


router.get('/settings', function(req, res) {
    req.getConnection(function(err,connection){

        connection.query('select * from SETTINGS',[],function(err,result){
            if(err) return res.status(400).json();

            return res.status(200).json(result);
        });
    });
});

//create
router.post('/addset', function(req, res) {

    var insert_data = req.body;

    req.getConnection(function(err,connection){

        connection.query('INSERT INTO SETTINGS SET ?',[insert_data],function(err,rows,result){
            if (!err){
                var response = [];

                if (rows.length !== 0) {
                    response.push({'result' : 'success', 'data' : rows});
                } else {
                    response.push({'result' : 'error', 'msg' : 'No Results Found'});
                }

                res.setHeader('Content-Type', 'application/json');
                res.status(200).send(JSON.stringify(response));
            } else {
                res.status(400).send(err);
            }
        });
    });
});

the error which i got when in postman

{
    "code": "ER_PARSE_ERROR",
    "errno": 1064,
    "sqlMessage": "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1",
    "sqlState": "42000",
    "index": 0,
    "sql": "INSERT INTO SETTINGS SET "
}

Sherif Adel

i solved this issue by modifying content-type to json in postman

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

Read timed out. error while sending a POST request to a node.js API

From

How is an HTTP POST request made in node.js?

From Dev

node js sending post request through jquery form data error

From Dev

Node.js (with express & bodyParser): unable to obtain form-data from post request

From Dev

While creating a REST API in node, How can I stream http response from a request made to an external website to the original api call?

From Dev

Node.js express - body of POST request is always empty

From Dev

Request parameters while consuming a REST API in node.js

From Dev

Node.js express routing POST Request empty body, using body-parser

From Dev

Node express.js respond to post request

From Dev

How to return data from post request in Node js?

From Dev

node.js, express, how to get data from body form-data in post request

From Dev

Error while sending POST request with data

From Dev

Error while sending data with POST request in Android

From Dev

Returning empty object from POST request (mithril w/ Node and Express)

From Dev

Storing data stream from POST request in GridFS, express, mongoDB, node.js

From Dev

jQuery + node.js express POST request

From Dev

POST data to a JSON REST API using request-promise of node

From Dev

Express calling an api inside of a post request

From Dev

How to read the data from node js post ajax request?

From Dev

Questions about POST request to node.js from ajax (Error)

From Dev

Get data from mongo while post request in node?

From Dev

Node express request goes to other endpoint in router

From Dev

Send Post request in Node js Rest API

From Dev

Express js error while trying to create a post request

From Dev

Post request from React to Node/ Express is console logging an empty object

From Dev

node express return request body empty by using API fetch POST

From Dev

Empty body POST request express

From Dev

Node.JS - Express request returns undefined when when submitting a post request from a form

From Dev

Why does Node.js Express call request "close" on POST request with data before request is closed?

Related Related

  1. 1

    Read timed out. error while sending a POST request to a node.js API

  2. 2

    How is an HTTP POST request made in node.js?

  3. 3

    node js sending post request through jquery form data error

  4. 4

    Node.js (with express & bodyParser): unable to obtain form-data from post request

  5. 5

    While creating a REST API in node, How can I stream http response from a request made to an external website to the original api call?

  6. 6

    Node.js express - body of POST request is always empty

  7. 7

    Request parameters while consuming a REST API in node.js

  8. 8

    Node.js express routing POST Request empty body, using body-parser

  9. 9

    Node express.js respond to post request

  10. 10

    How to return data from post request in Node js?

  11. 11

    node.js, express, how to get data from body form-data in post request

  12. 12

    Error while sending POST request with data

  13. 13

    Error while sending data with POST request in Android

  14. 14

    Returning empty object from POST request (mithril w/ Node and Express)

  15. 15

    Storing data stream from POST request in GridFS, express, mongoDB, node.js

  16. 16

    jQuery + node.js express POST request

  17. 17

    POST data to a JSON REST API using request-promise of node

  18. 18

    Express calling an api inside of a post request

  19. 19

    How to read the data from node js post ajax request?

  20. 20

    Questions about POST request to node.js from ajax (Error)

  21. 21

    Get data from mongo while post request in node?

  22. 22

    Node express request goes to other endpoint in router

  23. 23

    Send Post request in Node js Rest API

  24. 24

    Express js error while trying to create a post request

  25. 25

    Post request from React to Node/ Express is console logging an empty object

  26. 26

    node express return request body empty by using API fetch POST

  27. 27

    Empty body POST request express

  28. 28

    Node.JS - Express request returns undefined when when submitting a post request from a form

  29. 29

    Why does Node.js Express call request "close" on POST request with data before request is closed?

HotTag

Archive