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

**URL:** <https://community.postman.com/t/the-console-log-doesn-t-match-with-response-body-of-api-request/64861>\
**Category:** Help Hub\
**Tags:** tests, scripts\
**Created:** [August 1, 2024, 8:22am UTC](https://community.postman.com/t/the-console-log-doesn-t-match-with-response-body-of-api-request/64861 "2024-08-01T08:22:26Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![altimetry-geoscient2](https://avatars.discourse-cdn.com/v4/letter/a/8797f3/32.png) [@altimetry-geoscient2](https://community.postman.com/u/altimetry-geoscient2)\
**Post date:** [August 1, 2024, 8:22am UTC](https://community.postman.com/t/the-console-log-doesn-t-match-with-response-body-of-api-request/64861/1 "2024-08-01T08:22:26Z")

</div>

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

```auto
{
    "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

```auto
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

 ![image](https://us1.discourse-cdn.com/getpostman/original/3X/d/d/ddd64c9071cff894e7c58781bc017d30fbccb82d.png)

---

<div class="post-metadata">

**Author:** ![danny-dainton](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/danny-dainton/32/29401_2.png) [@danny-dainton](https://community.postman.com/u/danny-dainton)\
**Post date:** [August 1, 2024, 9:41am UTC](https://community.postman.com/t/the-console-log-doesn-t-match-with-response-body-of-api-request/64861/2 "2024-08-01T09:41:12Z")

</div>

Hey @altimetry-geoscient2 👋

Welcome to the Postman Community! :postman:

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

> [@altimetry-geoscient2](#):
>
> `var responseJSON = actualResponse.json();`

Wouldn’t this work for you:

```auto
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");
});

```

---

<div class="post-metadata">

**Author:** ![altimetry-geoscient2](https://avatars.discourse-cdn.com/v4/letter/a/8797f3/32.png) [@altimetry-geoscient2](https://community.postman.com/u/altimetry-geoscient2)\
**Post date:** [August 1, 2024, 11:04am UTC](https://community.postman.com/t/the-console-log-doesn-t-match-with-response-body-of-api-request/64861/3 "2024-08-01T11:04:03Z")

</div>

Hi @danny-dainton

actualResponse is referring to pm.response

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

```auto
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"])
    });
}

```

---

<div class="post-metadata">

**Author:** ![danny-dainton](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/danny-dainton/32/29401_2.png) [@danny-dainton](https://community.postman.com/u/danny-dainton)\
**Post date:** [August 1, 2024, 3:36pm UTC](https://community.postman.com/t/the-console-log-doesn-t-match-with-response-body-of-api-request/64861/4 "2024-08-01T15:36:50Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![michaelderekjones](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/michaelderekjones/32/14819_2.png) [@michaelderekjones](https://community.postman.com/u/michaelderekjones)\
**Post date:** [August 1, 2024, 6:05pm UTC](https://community.postman.com/t/the-console-log-doesn-t-match-with-response-body-of-api-request/64861/5 "2024-08-01T18:05:04Z")

</div>

> [@altimetry-geoscient2](#):
>
> ```auto
> 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"])
> });
> }
> 
> ```

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.

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

```

---

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/system/32/29796_2.png) [@system](https://community.postman.com/u/system)\
**Post date:** [August 31, 2024, 6:05pm UTC](https://community.postman.com/t/the-console-log-doesn-t-match-with-response-body-of-api-request/64861/6 "2024-08-31T18:05:20Z")

</div>

Closed on August 31, 2024, 6:05pm UTC

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