GET request with raw data, query string, arguments in express - deesoft service

GET request with raw data, query string, arguments in express

Deepak Tailor Image
Deepak Tailor - Nov 29 2021
GET request with raw data, query string, arguments in express

in the article, how to send get request with json raw data and params arguments or query string. then how to get params arguments and query string data in express

1. Create a new request in postman

2. Select GET method type

3. Pass api get url and raw data

Get url

Ex. http://localhost:3000/update_student/1

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

Create a new route for handle this request.

router.get('/update_student/:id', (req, res, next) => {
    console.log('update student get request');
});

Get the params data first

// get param arguments
const student_id = req.params.id;
console.log(student_id);

get the body raw data

// get body raw data
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_id;
get request with using query string

how to get query string data, query string data available in the query parameter in express.

url : http://localhost:3000/update_student?student_id=1&roll_no=12345

get query string data in express

In request query, we get the complete query string data. We get this in the format of the object.

Create new route
router.get('/update_student', (req, res, next) => {
get query string data
// get query string data
const student_id = req.query.student_id;
const roll_no = req.query.roll_no;

console.log(student_id);
console.log(roll_no);
complete get request with query string and raw data
router.get('/update_student', (req, res, next) => {

    // get query string data
    const student_id = req.query.student_id;
    const roll_no = req.query.roll_no;

    console.log(student_id);
    console.log(roll_no);

    // get body raw data
    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_id;

    console.log(email);
});

1. req.body for fetch all body data

2. req.params for fetch all params data

3. req.query for fetch all query string data

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