Creating GET and POST HTTP Requests

Last Updated : 17 Jan, 2026

GET and POST requests enable clients to retrieve and send data to servers using standard HTTP methods.

  • GET retrieves data without modifying server state.
  • POST submits data for processing or storage.
  • Data is exchanged using request–response model.
  • Common in REST APIs and web applications.

HTTP Request

When building web applications, clients and servers communicate using HTTP requests, mainly through GET and POST methods.

  • GET Request: Used to retrieve data from the server without sending a request body.
  • POST Request: Used to send data to the server in the request body for processing or storage.

Node.js HTTP Server

A Node.js HTTP server handles client requests and sends responses using the built-in http module, enabling backend functionality without external frameworks.

  • Created using the http core module.
  • Listens on a specific port.
  • Handles GET and POST requests.
  • Follows the request–response model.
  • Lightweight and event-driven.

Setting up a simple HTTP server in Node.js that listens on port 8080:

JavaScript
const http = require("http");
const users = [
    { id: 1, name: "Nicol" },
    { id: 2, name: "James" },
    { id: 3, name: "Marry" }
];

const server = http.createServer((req, res) => {
    res.setHeader("Content-Type", "application/json");
    res.write(JSON.stringify({ users }));
    res.end();
});

server.listen(8080, () => console.log("Server open on 8080"));
Screenshot-2026-01-17-115212

To Check the Browser is sending Get Request

To verify that the browser sends a GET request, check the request method using req.method === "GET" on the server.

JavaScript
const http = require("http");
const users = [
    { id: 1, name: "Nicol" },
    { id: 2, name: "James" },
    { id: 3, name: "Marry" }
];

const server = http.createServer((req, res) => {
   if(req.method === "GET"){
    res.setHeader("Content-Type", "application/json");
    res.write(JSON.stringify({ users }));
    return res.end();
    }
});

server.listen(8080, () => console.log("Server open on 8080"));
Screenshot-2026-01-17-120132

Testing with Postman

Testing with Postman: Postman allows you to easily test GET and POST requests by sending HTTP requests and inspecting server responses.

  • Select the request method (GET or POST) and enter the server URL.
  • Send the request to view the response data and status.

GET Request: Open Postman, select GET as the request type.

Enter the URL as http://localhost:8080.

Click Send to see the data returned from the server (the list of users).

POST Request: In Postman, select POST as the request type.

Use the same URL (http://localhost:8080).

In the body, select raw and JSON as the format, and send data such as:

{ "id": 1, "name": "Nicol" }
JavaScript
const http = require("http");

const users = [
    { id: 1, name: "Nicol" },
    { id: 2, name: "James" },
    { id: 3, name: "Marry" }
];

const server = http.createServer((req, res) => {
    // GET: Send empty request via client, receive data from server
    if (req.method === "GET") {
        res.setHeader("Content-Type", "application/json");
        res.end(JSON.stringify({ users }));
    }

    // POST: Send data via client, receive response from server
    else if (req.method === "POST") {
        let data = "";

        req.on("data", (chunk) => {
            data += chunk;
        });

        req.on("end", () => {
            console.log(data);

            data = JSON.parse(data);
            const user = users.find((u) => u.id === data.id);

            res.setHeader("Content-Type", "application/json");
            res.end(JSON.stringify({ user }));
        });
    }
});

server.listen(8080, () => {
    console.log("Server is running on port 8080");
});
Screenshot-2026-01-17-123819
POST

Click Send and you will receive a response from the server confirming that the data was received.

Handling GET and POST Requests in Node.js

Node.js handles HTTP requests by checking the request method and responding accordingly. The most common methods are GET for retrieving data and POST for sending data to the server.

GET Request Handling

Checks for a GET method and returns JSON data without a request body.

  • The request method is checked using req.method === "GET".
  • If the request is a GET request, the server responds with JSON data.
  • GET requests do not carry data in the request body.

POST Request Handling

Receives body data in chunks, processes it, and sends a response.

  • POST requests send data to the server using the request body.
  • Incoming data is received in chunks using req.on("data").
  • The complete request body is processed once req.on("end") is triggered.
  • The server can then parse the data and send an appropriate response.

Example: Sending Data Using POST (Postman)

Request

  • Method: POST
  • URL: http://localhost:8080
  • Body (raw - JSON):
{  "id": 1,  "name": "Nicol"}

Server Response

{  "message": "Data received successfully",  "data": {    "id": 1,    "name": "Nicol"  }}

Error Handling in POST Requests

Error handling validates POST data and returns clear error responses.

  • Error handling ensures the server responds gracefully to invalid or missing data.
  • Input validation should be performed before processing the request.
  • Clear error messages help clients understand what went wrong.

Example: Handling Missing Data

if (!data.id || !data.name) {  res.writeHead(400, { "Content-Type": "application/json" });  res.end(JSON.stringify({ error: "Missing ID or name" }));}

Key Takeaways

  • GET requests retrieve data without a request body.
  • POST requests send data in the request body.
  • Node.js handles requests asynchronously in chunks.
  • Postman helps test and inspect HTTP requests and responses.
Comment