The console.log doesn`t match with Response Body of API request

Hi, I am getting below response body of an api request

{
    "status": false,
    "statusMessage": "ER_BAD_FIELD_ERROR: Unknown column '1as' in 'where clause'",
    "statusCode": "ER_BAD_FIELD_ERROR"
}

while i put assertion on this request i am getting error as it says it does not match the value of statusCode and when i console the json response its giving me different response

Below is my assertion test

pm.test("Validate status message 'ER_BAD_FIELD_ERROR'", function () {
    var responseJSON = actualResponse.json();

    console.log(responseJSON, "error")
    pm.expect(responseJSON.statusCode).to.equal("ER_BAD_FIELD_ERROR");
});

And below is the console value

Hey @altimetry-geoscient2 :wave:

Welcome to the Postman Community! :postman:

Would you be able to confirm what actualResponse.json() is referencing in that test?

Wouldn’t this work for you:

pm.test("Validate status message 'ER_BAD_FIELD_ERROR'", function () {
    var responseJSON = pm.response.json();

    console.log(responseJSON, "error")
    pm.expect(responseJSON.statusCode).to.equal("ER_BAD_FIELD_ERROR");
});

Hi @danny-dainton

actualResponse is referring to pm.response

As i have used this response in a function parameter as below

function invalidAddress(actualResponse){
pm.response = actualResponse

pm.test("Validate status message 'Resource Not Found'", function () {
    var responseJSON = actualResponse.json();
    pm.expect(responseJSON.statusMessage).to.be.oneOf(["Resource Not Found", "Validation Error" ])
    });
}

I don’t really have the full script or context here.

Could you include all the relevant information please?

Is that single request used to test multiple different error scenarios? How’s the function being called? Do you have repeated variable usage in that script and the console message is from elsewhere?

That function doesn’t look right at all.

You are passing in a parameter called actualResponse.

The next line “pm.response = actualResponse” appears to be backwards.

pm.response is a Postman function and is unmutable. You’re trying to overwrite it with the actualResponse value.

I think you meant…

let actualResponse = pm.response

But you can’t really do that either, as you are already passing in a parameter called actualResponse, so it can’t have the same name.

With the info provided so far, I can see no good reason for this to be a function as you don’t seem to be using the parameter you are passing into the function.

Create this is a normal test before you try to create a re-usable function.

const response = pm.response.json();
pm.test("statusMessage = ER_BAD_FIELD_ERROR", function () {
   pm.expect(response.statusCode).to.equal("ER_BAD_FIELD_ERROR");
});
1 Like

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