Retrieve the POST query parameters using Express
Found out how to retrieve the POST query parameters using Express
POST query parameters are sent by HTTP clients for example by forms, or when performing a POST request sending data.
How can you access this data?
If the data was sent as JSON, using Content-Type: application/json
, you will use the express.json()
middleware:
const express = require('express')
const app = express()
app.use(express.json())
If the data was sent using Content-Type: application/x-www-form-urlencoded
, you will need to use the express.urlencoded()
middleware:
const express = require('express')
const app = express()
app.use(express.urlencoded({
extended: true
}))
In both cases you can access the data by referencing it from Request.body
:
app.post('/form', (req, res) => {
const name = req.body.name
})
Note: older Express versions required the use of the
body-parser
module to process POST data. This is no longer the case as of Express 4.16 (released in September 2017) and later versions.
→ I wrote 17 books to help you become a better developer, download them all at $0 cost by joining my newsletter
→ JOIN MY CODING BOOTCAMP, an amazing cohort course that will be a huge step up in your coding career - covering React, Next.js - next edition February 2025