Testing a POST request using a GET request

I am new to API testing and looking to validate a POST request. The request creates a user account and I am trying to write a test to GET the user details and confirm they match the POST body.

I used the Postman bot tool and it suggested the below script, however the GET request keeps coming back empty. Is this test even possible? Am I approaching this the wrong way? Any help greatly appreciated as the bot is putting me round in circles!

// Test for status code 200
pm.test("Response status code is 200", function () {
  pm.response.to.have.status(200);
});

// POST request expected response data
const expectedUser = {
    "id": "API123",
    "name": "API Test User",
    "email": "[email protected]",
    "role": 0
};

// Send GET request to retrieve new user details
pm.sendRequest({
    url: 'https://xxxxx/api/[email protected]',
    method: 'GET'
}, function (err, response) { 
    if (err) {
        console.error(err);
        return;
    }
    // Parse the response body
    const responseBody = response.json();

// POST request expected response data
const expectedUser = {
    "oid": "API123",
    "name": "API Test User",
    "email": "[email protected]",
    "role": 0
};

    // Compare the retrieved data with the expectedUser
    pm.test('User details match', function () {
        pm.expect(responseBody.oid).to.eql(expectedUser.id);
        pm.expect(responseBody.name).to.eql(expectedUser.name);
        pm.expect(responseBody.email).to.eql(expectedUser.email);
        pm.expect(responseBody.role).to.eql(expectedUser.role);
    });
});

Craft a normal GET request using the GUI and get that working first.

Once you have that working, look at the console log to see what is being sent and compare to the console log for the sendRequest().

This should show you if there are any differences or whether you’ve missed something in the GET request.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.