I need to receive simple text on my POST route:
'use strict';
const express = require('express');
const functions = require('../../utils/functions');
let router = express.Router();
router.post('/ses', function(req, res) {
res.send(req.body)
});
module.exports = router;
It returns {}
I found some people using body parse and I tried to use like this:
const bodyParser = require('body-parser')
router.use(bodyParser.json());
// and tried it too. Not together, of course
router.use(bodyParser.urlencoded({ extended: false }))
It didn't work. I'm not a NodeJS pro. How can I get it working?
Perhaps bodyParser.text() is what you are looking for.
See Expressjs raw body
Related
So, when i make a Post request to the ExpressJS api with postman (localhost:8080?test=test), i should get this from the api: {test: test} or something right? But i get this: {}.
Heres my code:
Api:
const express = require('express');
const http = require('http');
const path = require('path');
const app = express();
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
app.use(cookieParser())
app.use(bodyParser.urlencoded({extended: true}));
app.use(bodyParser.json())
app.use(express.static(__dirname + '../../../src/'));
app.get('/*', (req,res) => res.sendFile(path.join(__dirname)));
const server = http.createServer(app);
server.listen(port,() => {
console.log('Running...');
})
app.post('/', function(req,res){
let data = JSON.stringify(req.body);
console.log(data);
res.send(data)
})
Can you help me?
Edit:
Printscreen
Inside post endpoint you are console logging req.body which is actually {}. According to the endpoint you are receiving data as query params. Therefore you have to use req.query instead of req.body to capture the data.
I have the following in my index.js:
app.use(bodyParser.json());
But Stripe webhooks want this:
Match the raw body to content type application/json
If I change my index.js to the following:
app.use(bodyParser.raw({type: 'application/json'}));
It works fine. But all my other API routes will not work anymore. Here is my route:
router.route('/stripe-events')
.post(odoraCtrl.stripeEvents)
How can I change to the raw body for only this api route?
You can access to both at the same time by doing this:
app.use(bodyParser.json({
verify: (req, res, buf) => {
req.rawBody = buf
}
}))
Now the raw body is available on req.rawBody and the JSON parsed data is available on req.body.
Divide them into two routers '/api' and '/stripe-events' and indicate bodyParser.json() only for the first one:
stripeEvents.js
const express = require('express')
const router = express.Router()
...
router.route('/stripe-events')
.post(odoraCtrl.stripeEvents)
module.exports = router
api.js
const express = require('express')
const router = express.Router()
...
router.route('/resource1')
.post(addResource1)
router.route('/resource2')
.post(addResource2)
module.exports = router
const stripeEventsRouter = require('./routers/stripeEvents';
const apiRouter = require('./routers/api';
apiRouter.use(bodyParser.json());
stripeEventsRouter.use(bodyParser.raw({type: 'application/json'}));
app.use('/api', stripeEventsRouter);
app.use('/api', apiRouter);
My application has post route that accepts the data from postman client. I have written following code to retrieve value of form and print it:
var express = require('express');
req.app.use(express.urlencoded());
req.app.use(express.json());
console.log('req.body.name --> ' + req.body.name);
Above code prints req.body.name --> undefined rather than name value given in field name
I also tried following code:
var express = require('express');
const bodyParser = require('body-parser');
req.app.use(bodyParser.urlencoded({ extended: true }));
console.log('req.body.name --> ' + req.body.name);
Above code too prints req.body.name --> undefined rather than name value given in field name.
Can anyone please guide me on to resolve the issue in retrieving field name?
Used multer to solve this based on inputs from req.body is empty on express POST call.
Posting to benefit others.
var multer = require('multer');
var upload = multer() ;
app.post('/test', upload.array(), function (req, res, next) {
console.log(req.body.name);
});
Cheers :)
In your code, req is used, but req is not declared.
You wrote that you used POST, but you didn't write an entry point for POST.
Please try the following code:
const express = require('express');
const bodyParser = require('body-parser');
const app = express();
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json({ extended: true }));
app.post('/users', (req, res) => {
console.log(req.body);
console.log(req.body.name);
res.send(req.body.name);
});
app.listen(3000);
The port number is not necessarily 3000.
Use your Postman configuration.
In your code, proper imports are not there.
1. For Any node application these imports are mandatory
const express = require('express');
const bodyParser = require('body-parser');
const app = express();
2. Use these imports properly like
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json({ extended: true }));
3. Then use any Http Method like Get, Post, Put, and Delete
Example -
app.post(‘/sample’, (req, res) => {
// name must be in your request body while requesting for the above API
console.log(req.body.name);
res.send(req.body.name);
});
4. Add the port lister on which Application should run
app.listen(9000);
Have a problem with using another method in class. I'm working with express framework and have to use Ecma Script 6. When I try to use method in the same class I have an error
const express = require('express');
var bp = require('body-parser');
const fd = express();
fd.use(bp.json());
fd.use(bp.urlencoded({ extended: true }));
fd.post('/', function(req, res){
console.log(req.body);
});
The relevant part of my app.js is as follows
var express = require('express');
var bodyParser = require('body-parser');
var app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
var routes = require('./config/routes);
app.use('/', routes);
My route file is:
var express = require('express');
var router = express.Router();
var upgradesController = require('../../app/controllers/upgrades.server.controller');
// This should receive POST requests
router.post('/api/upgrades/device', upgradesController.create);
module.exports = router;
And finally my controller is
exports.create = function(req, res) {
res.send(req.body);
}
But this sends nothing. It's always an empty JSON value. I'm using PostMan for testing:
What is happening?
You're sending form-data, switch to x-www-form-urlencoded instead. You can also send "raw", and input valid JSON.