0

Request Body:

    {
    "StudentInfo":{
    "Student":[
    {
    "StudentId":"001"
    }
    }
    }

Response Body

{
"Studentinfo":{
"Student":[
{
"StudentId":"001",
"firstname":"John",
"Lastname":"Tac",
"DOB":"2015-02-10"
}
}
}

The fields in the response are stored in DB, if I change in the request to '002' then it has to pull 002 data and I need to validate through tests in Postman.

IAmMilinPatel I appreciate your answer. I do not understand //Write your database query here to get the student data based on the student ID. Can we write a select statement directly here?

// Function to query the database to get the student data
function getStudentDataFromDB(studentId) {
    // Write your database query here to get the student data based on the student ID
    // Return the student data as an object
    return {
        StudentId: "001",
        FirstName: "John",
        LastName: "Tac",
        DOB: "2015-02-10"
    };
}
2

1 Answer 1

0

To compare the response body with your database data in Postman tests, you can make use of the Postman's built-in test script feature. Here is a sample test script that can help you compare the response body with your database data:

// Get the response body
let responseBody = pm.response.json();

// Get the student ID from the request body
let studentId = pm.request.json().StudentInfo.Student[0].StudentId;

// Query the database to get the student data
let dbData = getStudentDataFromDB(studentId);

// Compare the response body with the database data
pm.test("Response body matches the database data", function () {
    pm.expect(responseBody.Studentinfo.Student[0].StudentId).to.equal(dbData.StudentId);
    pm.expect(responseBody.Studentinfo.Student[0].firstname).to.equal(dbData.FirstName);
    pm.expect(responseBody.Studentinfo.Student[0].Lastname).to.equal(dbData.LastName);
    pm.expect(responseBody.Studentinfo.Student[0].DOB).to.equal(dbData.DOB);
});

// Function to query the database to get the student data
function getStudentDataFromDB(studentId) {
    // Write your database query here to get the student data based on the student ID
    // Return the student data as an object
    return {
        StudentId: "001",
        FirstName: "John",
        LastName: "Tac",
        DOB: "2015-02-10"
    };
}

In this test script, the responseBody variable gets the response body from the API response, and the studentId variable gets the student ID from the request body. Then, the getStudentDataFromDB() function queries the database to get the student data based on the student ID, and returns the student data as an object. Finally, the test script compares the response body with the database data using the pm.expect() method.

Note that you will need to replace the getStudentDataFromDB() function with your actual database query to get the student data from your database. You can also modify the test script as per your requirements to handle different scenarios and conditions.

I hope this helps you in comparing the response body with your database data in Postman tests!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.