DEV Community

Hasan Zohdy
Hasan Zohdy

Posted on

12-Nodejs Course 2023: Request Data Types

Request Data Types

We can actually get data from the client in two ways, one from the url and the other from the request body (payload). Let's see how we can get data from the url.

Data from the URL

We earlier talked about the url structure, we can use the url structure to determine the data that we can receive in our server to act accordingly. we can get it from two places, the query string and the url parameters.

Query String

The query string is the part of the url that comes after the ? symbol. It is a list of key-value pairs separated by the & symbol. For example, in the url https://example.com?name=John&age=20, the query string is name=John&age=20 this data is converted in the server into an object. We can access this data in the server using the query property of the request object, which we will see later.

URL Parameters

The url parameters are the part of the url that comes after the / symbol. For example, in the url https://example.com/user/1, the url parameter is 1. We can access this data in the server using the params property of the request object, which we will see later as well.

Data from the Request Body

The request body is the data that we send to the server in the request body. We can send data in different formats, the most common ones are JSON and form data. We will see how to handle both of them.

JSON

JSON is the most common format to send data to the server. We can send JSON data in the request body. We can access this data in the server using the body property of the request object.

Form Data

Form data is another common format to send data to the server. We can send form data in the request body. We can access this data in the server using the body property of the request object as well.

The major difference between form data and JSON data is that form data can be sent in different formats, for example, x-www-form-urlencoded and multipart/form-data, which will allow the user to send files as well, which is not possible with JSON data.

Request Payload

Request data is sent to the server in the request body. The body is a part of the HTTP request. It is used to send data to the server. The data sent to the server is stored in a buffer. We can parse the data to get a better understanding of it.

Example of query string

Now let's see some example to illustrate how this works. We will use the query property of the request object to get the data from the query string.

We'll try this in our /users route which we created earlier, let's just update the controller function.

// src/app/users/controllers/list-users.ts

export default function listUsers(request) {
    const { query } = request;
    console.log(query);
}
Enter fullscreen mode Exit fullscreen mode

Now go to the browser and open the url http://localhost:3000/users?name=John&age=20. You should see the following output in the console.

{
    "name": "John",
    "age": "20"
}
Enter fullscreen mode Exit fullscreen mode

So this is how we can get data from the query string.

Example of url parameters

Now let's see some example to illustrate how this works. We will use the params property of the request object to get the data from the url parameters.

We'll try this in our /users/:id route which we created earlier, let's just update the controller function.

// src/app/users/controllers/get-user.ts

export default function getUser(request) {
    const { params } = request;
    console.log(params);
}
Enter fullscreen mode Exit fullscreen mode

Open now the users/route.ts file and add the following code.

// src/app/users/routes.ts
import getUser from './controllers/get-user';
import router from 'core/router';

router.get('/users/:id', getUser);
Enter fullscreen mode Exit fullscreen mode

Now go to the browser and open the url http://localhost:3000/users/1. You should see the following output in the console.

{
    "id": "1"
}
Enter fullscreen mode Exit fullscreen mode

So this is how we can get data from the url parameters.

The next chapter will be about the request object.

🎨 Project Repository

You can find the latest updates of this project on Github

😍 Join our community

Join our community on Discord to get help and support (Node Js 2023 Channel).

🎞️ Video Course (Arabic Voice)

If you want to learn this course in video format, you can find it on Youtube, the course is in Arabic language.

💰 Bonus Content 💰

You may have a look at these articles, it will definitely boost your knowledge and productivity.

General Topics

Packages & Libraries

React Js Packages

Courses (Articles)

Top comments (0)