Post request send with complex raw data in express

Deepak Tailor Image
Deepak Tailor - Nov 25 2021
Post request send with complex raw data in express

how to send simple json raw and complex, nested row data in post request then how to received json data in express

1. select post request in postman.

2. click to body tab and select Json in the dropdown box.

3. Create raw data like as first_name, last_name, mobile_no, email, password

Send simple raw data to express
{
    "first_name": "Deepak",
    "last_name": "Tailor",
    "mobile_no": "7742307462",
    "email_id": "deepaktailor@gmail.com",
    "password": "123456"
}

call json function in server file above to the route file

// use json data
app.use(express.json());

// router use
app.use(router);

do raw data print in the post route
all data available in the req.body function inside post request

Post request send with complex raw data in express

getting the single single variable in req.body

router.post('/', (req, res, next) => {
    
    // get row data in body
    const first_name    = req.body.first_name;
    const last_name     = req.body.last_name;
    const mobile_no     = req.body.mobile_no;
    const email         = req.body.email;
    const password      = req.body.password;
});
Send complex json raw data to express
{
    "first_name": "Deepak",
    "last_name": "Tailor",
    "mobile_no": "7742307462",
    "email_id": "deepaktailor@gmail.com",
    "password_data": {
        "password": "123456",
        "confirm_password": "123456"
    },
    "account_details" : {
        "bank_name" : "SBI BANK",
        "account_holder_name" : "DEEPAK TAILOR",
        "account_no" : "1234567890",
        "ifsc_code" : "000000",
        "address" : {
            "city" : "jaipur",
            "state":"rajasthan",
            "pin_code" : "000000"
        }
    }
}
Handle complex json data
// get row data in body
const first_name    = req.body.first_name;
const last_name     = req.body.last_name;
const mobile_no     = req.body.mobile_no;
const email         = req.body.email;
// const password      = req.body.password;

// single object data
const password = req.body.password_data.password
const confirm_password = req.body.password_data.confirm_password

// nested object data
const bank_name = req.body.account_details.bank_name;
const account_holder_name = req.body.account_details.account_holder_name;
const account_no = req.body.account_details.account_no;
const ifsc_code = req.body.account_details.ifsc_code;

const city = req.body.account_details.address.city;
const state = req.body.account_details.address.state;
const pin_code = req.body.account_details.address.pin_code;

console.log(bank_name);
console.log(state);
complex code to simplified code (compress code)
// get bank details
const bank_details = req.body.account_details;
const bank_name = bank_details.bank_name;
const account_holder_name = bank_details.account_holder_name;
const account_no = bank_details.account_no;
const ifsc_code = bank_details.ifsc_code;

// get bank address data
const address = req.body.account_details.address;
const city = address.city;
const state = address.state;
const pin_code = address.pin_code;

console.log(bank_name);
console.log(state);
Deepak Tailor Image
Deepak Tailor

My name is Deepak tailor as a fullstack developer. I have been in the IT industry (PHP, Nodejs, flutter) for the last 5 years. For professional and customize web development & app development, you can send inquiry on our email.
----
You can contact him at deepaktailor10@yahoo.in