I am using the light version of Postman 11.0.12 on a Windows 10 server.
My body values always returns undefined
.
If I use Query Params then key returns the correct value.
I suspect could it be this version of Postman? Based on setup I cannot used the web version nor desktop version on my Win 11 as I think Postman doesn’t work with 11 yet.
I’ve even copied the code for CURL and tried it via a CMD window but still same issue.
Am I missing something simple….
ExpressJS:
const sql = require('mssql');
const express = require('express');
const app = express();
const port = 5001;
//SQL settings
//--------------
// const config = {
// user: bobbeat,
// }
//insert stock
const postStock = `INSERT INTO [DEV].[dbo].[stocks]
(code]
,[ID])
VALUES `;
let resultRows = [];
function queryDB(tableParam, stockID, addStockParams) {
const sql = require("msnodesqlv8");
const connectionString = config;
let tableToQuery = tableParam ===
: tableParam === 'postStock' ? postStock + '(' + addStockParams + ')'
: 'No table found';
sql.query(connectionString, tableToQuery, (err, rows) => {
rows ? console.log('rows returned') : console.log('errors are ', err)
resultRows.push(rows);
});
}
app.get('/', (req, res) => {
res.send('Hello World! from nodejs')
})
//post stock
app.post('/api/v1/postStock', (req, res) => {
//cannot get whole body via req.body but query param works
//1
//let stockBody = [ "req.body.code"];
console.log(req.query.code);
//2
//post stock to db
//queryDB('postStock', null, req.query.code);
//return result
res.send('resultRows');
});
//monitoring feature
app.get('/running', (req, res) => {
res.send('Api is up and PM2 monitoring');
})
app.listen(port, () => {
console.log(`Example app listening on port ${port}`)
})
POSTMAN – raw and JSON – not working:
{
"code": "abc"
}
Result:
undefined
Query params – working:
http://localhost:5001/api/v1/postStock?code=abc
Result:
C:BuildRunJSTools>node getSQLapi.js
=> type object
Example app listening on port 5001
abc