SDET Unicorns

Write API Tests for HTTP POST Method

Table of Content

Let’s take a look at how to write API tests using JavaScript for the HTTP POST method.

So when working with the POST method, one of the key things to remember is to send the request data along with the request. Let’s take a look at an example of creating a new user using the POST method –

it('POST /users', () => {
  // data to send with the request
  const data = {
    email: `test-${Math.floor(Math.random() * 9999)}@mail.ca`,
    name: 'Test name',
    gender: 'Male',
    status: 'Inactive',
  };

  return request
    .post('users') // hitting the POST route
    .set('Authorization', `Bearer ${TOKEN}`) // setting token for authentication
    .send(data)
    .then((res) => {
      // validate the entire response data using Chai assertion
      expect(res.body.data).to.deep.include(data);
    });
});

So the above code will create a new user for us and will give a similar response back –

{
  code: 201,
  meta: null,
  data: {
    id: 1437,
    name: 'Test name',
    email: 'test-6243@mail.ca',
    gender: 'Male',
    status: 'Inactive',
    created_at: '2020-09-27T04:15:02.057+05:30',
    updated_at: '2020-09-27T04:15:02.057+05:30'
  }
}

There you go, that’s all we need to do to create an API test for HTTP POST method. ?

Check out this video to see a detailed explanation on how to work with HTTP POST method:

You can also clone the GitHub repo to access this code


To learn more about API testing, check out my free tutorial series here –

JavaScript API Automation Testing Tutorial Series

My Resources

Thrive Suite: The all-in-one WordPress theme I used to create my blog.
Jasper AI: My favorite AI writing tool.
Surfer SEO:  A tool to help you rank your content on the first page of Google.

Write Content Faster

5/5

Write blog posts that rank for affiliate terms!

Join our mailing list

Unlock the Secrets to Becoming a SDET Pro in the Industry!

Stay ahead of the curve! Join our mailing list for exclusive insights, tips, and the latest industry updates delivered straight to your inbox

Table of Content

Related Post

7 Principles of Software Testing

7 Principles of Software Testing with Examples

Software testing plays a crucial role in the software development life cycle as it helps ensure the quality, reliability, and performance of the final product. Software testing is not just about finding defects; it also provides valuable insights into the overall quality of the software. By conducting effective software testing, it shows how well the application behaves in different scenarios and whether it meets user expectations.

Read More »
Differences Between CI/CD and DevOps

Key Differences Between CI/CD and DevOps

Software development is far from a simple journey that involves merely executing code and launching applications. It often presents challenges, as it’s rare to get everything right from the start. Each time you implement new or frequent code changes, you inadvertently introduce potential issues that can be time-consuming to resolve. This complexity necessitates a structured approach to ensure smooth production.

Read More »