Quantcast
Channel: Express.js req.body undefined - Stack Overflow
Viewing all articles
Browse latest Browse all 54

Answer by Rafael Fernando Fiedler for Express.js req.body undefined

$
0
0

Firsl of all, ensure you are applying this middleware (express.urlencoded) before routes.

let app = express();//response as Jsonapp.use(express.json()); //Parse x-www-form-urlencoded request into req.bodyapp.use(express.urlencoded({ extended: true }));     app.post('/test',(req,res)=>{    res.json(req.body);});

The code express.urlencoded({extended:true}) only responds to x-www-form-urlencodedposts requests, so in your ajax/XMLHttpRequest/fetch, make sure you are sending the request.setRequestHeader('Content-type', 'application/x-www-form-urlencoded'); header.

Thats it !


Viewing all articles
Browse latest Browse all 54

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>