Express POST request.body undefined - node.js

I am trying to make POST work with Express (4.13.3 version). when I print request.body.user, it says 'undefined'. I am using Chrome Poster to post my JSON request. Here is how my request looks
{
"user":"testUser",
"password":"test pwd"
}
the URL I use: http://localhost:4000/first
and my server.js file.
var express = require('express');
var bodyParser = require('body-parser');
var app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.post('/first', function (request, response) {
console.log('FIRST POST hello world');
console.log('req.body:' + request);
var user_name=request.body.user;
var password=request.body.password;
console.log("User name = "+user_name+", password is "+password);
response.end("yes");
});
var server = app.listen(4000, function() {
var host = server.address().address;
var port = server.address().port;
console.log('Example app listening at http://%s:%s', host, port);
});
when I post the request, here is what I see on my Node console.
Example app listening at http://:::4000
FIRST POST hello world
req.body:[object Object]
User name = undefined, password is undefined
Why I am not able to get my 'user' and 'password' values here from my request? I am getting 'undefined' for both of these variables.

try this:
app.use(bodyParser());
if this still doesn't work change your request to this:
user=testUser&password=test+pwd
This is how the request body have to look using Chrome's "Advanced REST Client".

Related

Nodejs HTTP Request Body Value Console Log

I'm attempting to extract a part of an HTTP POST Request Body and log it to my console using nodejs. Here is what I have so far:
const express = require('express');
const request = require('request');
const bodyParser = require('body-parser');
var app = express();
app.use(bodyParser.json);
app.listen(PORT, function () {
console.log("Example app listening on port " + PORT);
});
app.post('/events', function(req, res) {
console.log(req.body);
});
I'm trying to log the 'challenge' value:
POST
"body": {
"type": "url_verification",
"token": "0Rwv2qjMozpy9MSHWM2jKf0q",
"challenge": "sW6u5bGBluzxVR79Inv04HlLGdrSxZttZ0dJrUF4E4smD8RWmbkU"
}
You will just reference the challenge field from the body
consoe.log(req.body.challenge)

Post request sent by CURL gets parsed correctly but when sent from postman, I get undefined data

I am learning Node.js as a result I am setting up a authentication service. I have an issue parsing the body from post request.
This is my index.js file
const express = require('express')
const bodyParser = require('body-parser')
const app = express()
const db = require('./queries')
const port = 3000
app.use(express.json());
app.use(bodyParser.json())
app.use(
bodyParser.urlencoded({
extended: true,
})
)
app.get('/', (request, response) => {
response.json({ info: 'Node.js, Express, and Postgres API' })
})
app.post('/login',function(req,res){
var username=req.body.username;
var password=req.body.password;
console.log("User name = "+username+", password is "+password);
res.end("yes");
});
This is printed on the console:
bash-3.2$ node index.js
App running on port 3000.
Username = undefined, password is undefined
But when i use CURL
curl --data "username=Jerry&password=jerry#example.com" http://localhost:3000/login
It works. Don't know why?
You have to change the postman header settings. Try changing the header content type value to application/json and change body to raw.

Can not post to server using node express

I am trying to post data from postman to my node server, I keep getting 404.
Is my code setup correctly to receive post to http://localhost:8080/back-end/test and if not how can I fix it ?
var express = require('express');
var request = require('request');
var nodePardot = require('node-pardot');
var bodyParser = require('body-parser');
var rp = require('request-promise');
var app = express();
var port = process.env.PORT || 8080;
// Start the server
app.listen(port);
app.use(bodyParser.json()); // support json encoded bodies
app.use(bodyParser.urlencoded({extended: true})); // support encoded bodies
console.log('Test server started! At http://localhost:' + port); // Confirms server start
var firstFunction = function () {
return new Promise (function (resolve) {
setTimeout(function () {
app.post('back-end/test.js', function (req, res) {
console.log(req.body);
var login = req.body.LoginEmail;
res.send(login);
resolve({
data_login_email: login
});
});
}, 2000);
});
};
I am posting LoginEmail and keep getting 404.
Move app.post() outside of the timeout, promise, and firstFunction.
There is no proceeding paths defined in your code, so the path must start with a /: /back-end/test.js. Don't forget the extension since you've defined it.

BodyParser data is undefined after AJAX call

The body parser body is {}. I've already done research and made sure that my ajax data key is set correctly as well as make sure the middleware is set up correctly as well. Here is my frontend ajax call
$.ajax({
type:"GET",
url:"/api",
data: {course:"MATH-226"},
success: function(data){ alert(data);}
});
And here is my backend server.js file:
'use strict'
const express = require('express');
const path = require('path');
const bodyParser = require('body-parser');
const alg = require('./app/algorithm.js');
const app = express();
app.use('/', express.static(path.join(__dirname, 'public')));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: true}));
app.get('/api', (req, res) => {
console.log(req.body);
alg.create(req.body.course, answer => res.send(answer));
});
let server = app.listen(3000, () => {
let host = server.address().address;
let port = server.address().port;
console.log('Example app listening at http://%s:%s', host, port);
});
You are using a GET request, so it's probably not being sent. If you need to send something, you can attach it as a header or include it in the query string of the url. If you want to send data, I would use a POST request.
Check out this article
How to send data in request body with a GET when using jQuery $.ajax()

NodeJS express SOAP

I'm consuming a soap request with express and bodyparser, however the request is not properly formatted on consumption i.e the first equals (=) sign is converted to colon (:) i.e
Below is my code:
var express = require("express");
var app = express();
var bodyParser = require("body-parser");
require('body-parser-xml')(bodyParser);
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.xml());
app.get('/',function(req,res){
res.send("hello");
});
app.post('/soap',function(req,res){
req.setEncoding('utf8');
var request = req.body;
console.log(request);
res.end("yes");
});
var server = app.listen(3000, function () {
var host = server.address().address
var port = server.address().port
console.log("App listening at http://%s:%s", host, port)
})
Got the answer, the client was sending the data using incorrect (text) content type. With postman, ensure you set content type to text/xml or application/xml.

Resources