Hi,
I need to write automated test for the API response. The response contains values which are same as what I have sent as an request. I need to ensure that what I sent is been added in the database and reflected back in the response with an ID created. Please confirm if the below test is correct?
const json = pm.response.json();
console.log(json);
let requestData= JSON.parse(request.data);
console.log(requestData);
pm.test(“Data Fields Test against request”,()=>{
pm.expect(json.Data).to.deep.equal(requestData.Data);
}
)
Can you post an example request and response please? So we can check that they are exactly the same (which would be unusual).
Please use the preformatted text option in the editor when posting code or JSON, so it stops everything from being aligned to the left (and so we can cut and paste). Please don’t post as a screenshot.
In your code, you have…
let requestData= JSON.parse(request.data);
Where is the “request” variable being set?
You have console logs for both variables, what is being shown there? That should be your first troubleshooting step.
You can parse the request body using the following, but please be aware that if you are using variables in your request body, then its not going to pull across the values and your assertion will fail.
const requestBody = JSON.parse(pm.request.body.raw);
const response = pm.response.json();
console.log(requestBody);
console.log(response);
pm.test('Request body and response match', () => {
pm.expect(requestBody).to.eql(response);
});