Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Express post request gives {}

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Edit:
Printscreen

>Solution :

Inside post endpoint you are console loging req.body which is actually {}. According to the endpoint you are recieving data as qury params. Therefore you have to use req.query instead req.body to capture the data.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading