POST Requests

Introduction

A POST request is an HTTP request used to send data to a server and create a new resource. Unlike a GET request, which only retrieves information, a POST request transfers data such as user details, form submissions, login credentials, product information, or file uploads to the server.

POST requests are commonly used in REST APIs for operations like user registration, login, creating records, placing orders, and submitting forms.

The data sent in a POST request is typically included in the request body and is often formatted as JSON, although other formats such as form data and XML are also supported.

For automation engineers, POST requests are essential for testing API endpoints that create new resources, validating request payloads, verifying response data, checking status codes, and ensuring backend services handle incoming data correctly.

In this tutorial, you’ll learn how to send POST requests using JavaScript with both the Fetch API and Axios.


What is a POST Request?

A POST request is an HTTP request used to send data to a server and create a new resource.

The request data is usually included in the request body.


Why Use POST Requests?

POST requests help developers:

  • Create new users.

  • Submit forms.

  • Log users into applications.

  • Create orders.

  • Add products.

  • Upload files.

  • Send API data.


Basic Syntax Using Fetch API

fetch(

    "API_URL",

    {

        method: "POST",

        headers: {

            "Content-Type":

                "application/json"

        },

        body: JSON.stringify({

            name: "John"

        })

    }

);

Basic Syntax Using Axios

import axios

from "axios";

await axios.post(

    "API_URL",

    {

        name: "John"

    }

);

Example 1: Create a New Post Using Fetch API

async function createPost() {

    const response =

        await fetch(

            "https://jsonplaceholder.typicode.com/posts",

            {

                method: "POST",

                headers: {

                    "Content-Type":

                        "application/json"

                },

                body: JSON.stringify({

                    title: "JavaScript",

                    body: "Learning POST Requests",

                    userId: 1

                })

            }

        );

    const data =

        await response.json();

    console.log(data);

}

createPost();

Sample Output

{
  id: 101,
  title: "JavaScript",
  body: "Learning POST Requests",
  userId: 1
}

Example 2: Create a User Using Axios

import axios

from "axios";

async function createUser() {

    const response =

        await axios.post(

            "https://jsonplaceholder.typicode.com/users",

            {

                name: "Alice",

                email: "alice@example.com"

            }

        );

    console.log(

        response.data

    );

}

createUser();

Sample Output

{
  id: 11,
  name: "Alice",
  email: "alice@example.com"
}

Example 3: Send Login Credentials

async function login() {

    const response =

        await fetch(

            "https://example.com/login",

            {

                method: "POST",

                headers: {

                    "Content-Type":

                        "application/json"

                },

                body: JSON.stringify({

                    username: "admin",

                    password: "password123"

                })

            }

        );

    console.log(

        response.status

    );

}

Note: https://example.com/login is a placeholder URL used for demonstration purposes.


Example 4: Send JSON Data

const product =

{

    name: "Laptop",

    price: 800

};

fetch(

    "https://jsonplaceholder.typicode.com/posts",

    {

        method: "POST",

        headers: {

            "Content-Type":

                "application/json"

        },

        body: JSON.stringify(product)

    }

);

Example 5: Verify Response Status

async function createResource() {

    const response =

        await fetch(

            "https://jsonplaceholder.typicode.com/posts",

            {

                method: "POST",

                headers: {

                    "Content-Type":

                        "application/json"

                },

                body: JSON.stringify({

                    title: "Test"

                })

            }

        );

    console.log(

        response.status

    );

}

createResource();

Sample Output

201

Automation Testing Examples

POST requests are widely used in API automation.

Example 1: Validate Status Code

const response =

    await axios.post(

        "https://jsonplaceholder.typicode.com/posts",

        {

            title: "Automation"

        }

    );

console.log(

    response.status

);

Sample Output

201

Example 2: Validate Response Data

const response =

    await axios.post(

        "https://jsonplaceholder.typicode.com/posts",

        {

            title: "Node.js"

        }

    );

console.log(

    response.data.title

);

Sample Output

Node.js

Example 3: Verify Resource ID

const response =

    await axios.post(

        "https://jsonplaceholder.typicode.com/posts",

        {

            title: "Testing"

        }

    );

console.log(

    response.data.id

);

Sample Output

101

Example 4: Measure Response Time

const start =

    Date.now();

await axios.post(

    "https://jsonplaceholder.typicode.com/posts",

    {

        title: "Performance"

    }

);

const end =

    Date.now();

console.log(

    end - start,

    "ms"

);

Sample Output

135 ms

Example 5: Validate Returned User ID

const response =

    await axios.post(

        "https://jsonplaceholder.typicode.com/posts",

        {

            userId: 5

        }

    );

console.log(

    response.data.userId

);

Sample Output

5

Common POST Request Components

ComponentPurpose
URLAPI endpoint
MethodPOST
HeadersDescribe request content
BodyData sent to server
ResponseServer result

Common Uses of POST Requests

POST requests are commonly used for:

  • User registration.

  • User login.

  • Form submission.

  • Creating products.

  • Creating orders.

  • Uploading files.

  • Payment processing.

  • Creating blog posts.

  • Creating support tickets.

  • API automation.


Real-World Automation Uses

Automation engineers use POST requests for:

  • API creation testing.

  • Authentication testing.

  • Database record creation.

  • Test data generation.

  • CRUD testing.

  • Regression testing.

  • Integration testing.

  • Backend validation.

  • Smoke testing.

  • Performance testing.


Common Mistakes

Forgetting the Request Body

A POST request usually requires data to be included in the request body.


Missing Content-Type Header

When sending JSON, include:

Content-Type: application/json

Not Converting Objects to JSON

When using the Fetch API, convert JavaScript objects using JSON.stringify().


Ignoring Response Validation

Always verify status codes and returned response data after sending a POST request.


Best Practices

  • Use async/await for cleaner code.

  • Validate status codes after every POST request.

  • Verify response body fields.

  • Store API endpoints in configuration files.

  • Keep request payloads reusable.

  • Handle exceptions using try...catch.

  • Separate API request logic from test assertions.


Conclusion

POST requests are one of the most important HTTP methods for interacting with REST APIs. They allow applications to send data to servers and create new resources such as users, products, and orders.

For automation engineers, POST requests are essential for validating create operations, testing authentication, generating test data, and verifying backend functionality. Mastering POST requests with the Fetch API and Axios is a key skill for building reliable and maintainable API automation frameworks.


Frequently Asked Questions (FAQs)

What is a POST request?

A POST request is an HTTP request used to send data to a server and create a new resource.


Where is the request data stored?

The data is typically sent in the request body.


Why is the Content-Type header important?

It tells the server the format of the request body, such as application/json.


Can I use both Fetch API and Axios for POST requests?

Yes. Both support POST requests and can send JSON or other types of data.


Why are POST requests important in automation testing?

They allow automation engineers to test resource creation, user registration, authentication, CRUD operations, and API workflows.


Key Takeaways

  • POST requests send data to a server.

  • They are commonly used to create new resources.

  • Request data is usually placed in the request body.

  • JSON is the most common data format for POST requests.

  • Include the Content-Type: application/json header when sending JSON.

  • Use JSON.stringify() with the Fetch API.

  • Axios automatically handles JavaScript objects as JSON.

  • Always validate response status codes and response data.

  • POST requests are fundamental for REST API automation testing.

  • Mastering POST requests is essential for JavaScript and Node.js API automation.