get json from a post request to a nodejs server - json

Where in the request object is the json?
For example I know I can use body-parser to do this
var express = require('express');
var app = express();
var bodyParser = require('body-parser');
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
app.post('/', function(req, res){
console.log(req.body)
res.json({ message: 'goodbye'})
})
And start my server and hit it with
curl -H "Cont/json" -X POST -d '{"username":"xyz"}' http://localhost:3000/
but is there a way to do it without the body parser includes? Can I just see the json in the request?

You could do it through node stream as below
app.post('/', function(req, res){
var body = "";
req.on("data", function (data) {
body += data;
});
req.on("end", function() {
console.log(JSON.parse(body));
res.json({ message: 'goodbye'})
});
})

Yep, you can
//pipe to any writable stream
req.pipe(process.stdout);
Sure if u want - u may save it to string using something like this
var tmpstr = "";
req.on("data", function (data) {
tmpstr += data;
});
req.on("end", function() {
//do stuff
console.log("\ndata length is: %d", tmpstr.length);
});

Related

req.body is saying undefined while running on postman

var express = require('express');
var mysql=require('mysql');
var bodyParser = require('body-parser');
var con=mysql.createConnection({
host:"localhost",
user:"root",
password:"",
database:"nodedb"
});
var app = express();
app.use(bodyParser.urlencoded({extended: false}));
app.use(bodyParser.json());
app.post('/reg', function (req, res) {
var name=req.body.user_name;
console.log(name);
con.query("SELECT * FROM users WHERE email = ?", [req.body.email],
function(err,result){
if(result.length > 0){
console.log('mail already exist');
res.json('mail already exist');
}
else{
con.query("INSERT INTO users (`name`,`email`,`contact`,`password`) VALUES (?,?,?,?) ", [req.body.user_name,req.body.email,req.body.contact_no, req.body.user_password],
function(err1,result1){
if(err1){
console.log('failed');
res.json(err1);
}
else{
console.log('1 recoed inserted');
res.json(result1);
}
})
}
})
}
req.body.user_name print undefined in console and null on postman. It
is working on web page but not on postman. What should i do now. I looked in many websites but i didn't get answer.
You need to select the correct header content type: x-www-form-urlencoded
Hope it helps.
In the body of postman, make sure you are using raw and make the Application type to Application/json, then you can mention
{
"user_name": "Your_name"
}
You might not have initialized the middleware for using the body parser. Here's how you can do it:
Put app.use(express.json()); in the entry point of your project.

nodeJS - make HTTPS request, sending JSON data

I would like to send an HTTPS POST from one nodeJS server to another. I have some JSON data I would like to send with this request (populated by a html form).
How can I do this? I am aware of https.request() but there does not seem to be an option to include JSON as a part of the query. From my research it seems possible with an HTTP request, but not an HTTPS request. How can I solve this?
const pug = require('pug');
var cloudinary = require('cloudinary');
var express = require('express');
var multer = require('multer');
var upload = multer({ dest: 'uploads/' });
var request = require('request');
var bodyParser = require('body-parser');
var options = {
hostname: 'ec2-54-202-139-197.us-west-2.compute.amazonaws.com',
port: 443,
path: '/',
method: 'GET'
};
var app = express();
var parser = bodyParser.raw();
app.use(parser);
app.set('view engine', 'pug');
app.get('/', upload.single('avatar'), function(req, res) {
return res.render('index.pug');
});
app.get('/makeRequest*', function(req, res) {
query = req['query'];
/*
Here, I would like to send the contents of the query variable as JSON to the server specified in options.
*/
});
You can send JSON data through a POST http request with the native https node module, as stated in the documentation
All options from http.request() are valid.
So, taking the http.request() example you can do the following:
var postData = querystring.stringify({
'msg' : 'Hello World!'
});
var options = {
hostname: 'www.google.com',
port: 80,
path: '/upload',
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Content-Length': Buffer.byteLength(postData)
}
};
var req = https.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.log(`problem with request: ${e.message}`);
});
// write data to request body
req.write(postData);
req.end();
You should edit postData to your desired JSON object
I believe the below is what you want. Using the request library. See comments in the code for my recommendations.
...
var options = {
hostname: 'ec2-54-202-139-197.us-west-2.compute.amazonaws.com',
port: 443,
path: '/',
method: 'POST',
json: true
};
...
//making a post request and sending up your query is better then putting it in the query string
app.post('/makeRequest', function(req, res) {
var query = req.body['query'];
//NOTE, you cannot use a GET request to send JSON. You'll need to use a POST request.
//(you may need to make changes on your other servers)
options.body = { payload: query };
request(options, function(err, response, body) {
if (err) {
//Handle error
return;
}
if (response.statusCode == 200) {
console.log('contents received');
}
});
});
as matt mentioned you need to use request
to send JSON object not JSON.Stringify so that at the server you can receive it using:
app.post('/makeRequest', function(req, res) {
console.log (req.body.param1);
}
Use the following code:
var request = require("request");
request({
'url':"http://www.url.com",
method: "POST",
json: true,
body: {'param1':'any value'}
}, function (error, resp, body) {
console.log ("check response.");
});

Parsing JSON in Express without BodyParser

I'm trying to write a simple express server that takes incoming JSON (POST), parses the JSON and assigns to the request body. The catch is I cannot use bodyparser. Below is my server with a simple middleware function being passed to app.use
Problem: whenever I send dummy POST requests to my server with superagent (npm package that lets you send JSON via terminal) my server times out. I wrote an HTTP server in a similar fashion using req.on('data')...so I'm stumped. Any advice?
const express = require('express');
const app = express();
function jsonParser(req, res, next) {
res.writeHead(200, {'Content-Type:':'application/json'});
req.on('data', (data, err) => {
if (err) res.status(404).send({error: "invalid json"});
req.body = JSON.parse(data);
});
next();
};
app.use(jsonParser);
app.post('/', (req, res) => {
console.log('post request logging message...');
});
app.listen(3000, () => console.log('Server running on port 3000'));
I think the problem like to get rawBody in express.
Just like this:
app.use(function(req, res, next){
var data = "";
req.on('data', function(chunk){ data += chunk})
req.on('end', function(){
req.rawBody = data;
req.jsonBody = JSON.parse(data);
next();
})
})
And you need catch the error when parse the string to json and need to judge the Content-type of the Req.
Good luck.
another way that worked with me by collecting all chunks into an array and parsing the concatenated chunks.
app.use("/", (req, res, next)=>{
const body = [];
req.on("data", (chunk) => {
console.log(chunk);
body.push(chunk);
});
req.on("end", () => {
const parsedBody = Buffer.concat(body).toString();
const message = parsedBody.split('=')[1];
console.log(parsedBody);
console.log(message);
});
console.log(body);
});
To get access to req.body this worked for me:
app.use(express.json({extended: false}));
In Express v4.16.0 onwards:
app.use(express.json())

Can't parse json with node.js

I am a node.js beginner and I am trying to read a json file, but when I'm running 'npm start' in the terminal I get this error:
undefined:3462
SyntaxError: Unexpected end of input
at Object.parse (native)
at /Users/alonbond/node_apps/analoc_2/analoc/routes/index.js:15:20
at fs.js:334:14
at FSReqWrap.oncomplete (fs.js:95:15)
this is index.js:
var express = require('express');
var fs = require('fs');
var app = express.Router();
/* GET home page. */
app.get('/', function(req, res, next) {
console.log('Welcome to Express.js');
res.render('index', { title: 'Express' });
});
/* GET json */
app.get('/analoc/', function(req, res) {
fs.readFile('./sample_data.json', function(error, data){
jsonObj = JSON.parse(data);
res.send('THE DATA: ', jsonObj);
});
});
module.exports = app;
Any help?
thanks!
readFile is the asynchronous version. You should either just use readFileSync, or rewrite it to be properly asynchronous.
console.log('analoc request');
var fs = require('fs');
fs.readFile('./files/sample_data.json', function(err,config){
console.log('Config: ' + JSON.parse(config));
});
Or:
var config = fs.readFileSync('./files/sample_data.json');
console.log('Config: ' + JSON.parse(config));
readFile doesn't have a return value. You are trying to parse "undefined" as if it were JSON. The file is passed to the callback function after it has been read.
fs.readFile('./files/sample_data.json', function (err, data) {
if (err) throw err;
var config = JSON.parse(data);
console.log('Config: ', config);
});

RESTify on Node.js POST body / json

I am in need of help. I am POSTing json data to my node server. The node server is using RESTify for its API. I am having trouble getting req.body.name from the body of the posted data.
The posted data contains a json body. In it i have keys such as name, date, address, email, etc.
I want to get the name out of the json body. I am trying to do req.body.name but it is not working.
I have also included server.use(restify.bodyParser()); and it is not working.
I am able to req.params.name and assign a value. But if I POST json data like: {'food': 'ice cream', 'drink' : 'coke'}, I am getting undefined. However, If I do req.body, I get the full json body posted. I want to be able to specifically get an item like 'drink' and have that show on console.log.
var restify = require('restify');
var server = restify.createServer({
name: 'Hello World!',
version: '1.0.0'
});
server.use(restify.acceptParser(server.acceptable));
server.use(restify.jsonp());
server.use(restify.bodyParser({ mapParams: false }));
server.post('/locations/:name', function(req, res, next){
var name_value = req.params.name;
res.contentType = 'json';
console.log(req.params.name_value);
console.log(req.body.test);
});
server.listen(8080, function () {
console.log('%s listening at %s', server.name, server.url);
});
If you want to use req.params, you should change:
server.use(restify.plugins.bodyParser({ mapParams: false }));
to use true:
server.use(restify.plugins.bodyParser({ mapParams: true }));
Have you tried using the standard JSON library to parse the body as a json object? Then, you should be able to grab whatever property you need.
var jsonBody = JSON.parse(req.body);
console.log(jsonBody.name);
In addition to below answer . The latest syntax in restify 5.0 has been change .
All the parser that you are looking for is inside restify.plugins instead of restify use restify.plugins.bodyParser
The method to use it is this.
const restify = require("restify");
global.server = restify.createServer();
server.use(restify.plugins.queryParser({
mapParams: true
}));
server.use(restify.plugins.bodyParser({
mapParams: true
}));
server.use(restify.plugins.acceptParser(server.acceptable));
var restify = require('restify')
const restifyBodyParser = require('restify-plugins').bodyParser;
function respond(req, res, next) {
console.log(req.body)
const randomParam = req.body.randomParam
res.send(randomParam);
next();
}
var server = restify.createServer();
server.use(restifyBodyParser());
server.post('/hello/:name', respond);
server.head('/hello/:name', respond);
server.listen(8080, function() {
console.log('%s listening at %s', server.name, server.url);
});
... Is what worked for me with restify version 8.3.2
you must use req.params with bodyParser active.
var restify = require('restify');
var server = restify.createServer({
name: 'helloworld'
});
server.use(restify.bodyParser());
server.post({path: '/hello/:name'}, function(req, res, next) {
console.log(req.params);
res.send('<p>Olá</p>');
});
server.get({path: '/hello/:name', name: 'GetFoo'}, function respond(req, res, next) {
res.send({
hello: req.params.name
});
return next();
});
server.listen(8080, function() {
console.log('listening: %s', server.url);
});