# JSONError response without response body

**URL:** <https://community.postman.com/t/jsonerror-response-without-response-body/42272>\
**Category:** Help Hub\
**Created:** [December 21, 2022, 2:05pm UTC](https://community.postman.com/t/jsonerror-response-without-response-body/42272 "2022-12-21T14:05:22Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![rik.rodel1997](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/rik.rodel1997/32/14261_2.png) [@rik.rodel1997](https://community.postman.com/u/rik.rodel1997)\
**Post date:** [December 21, 2022, 2:05pm UTC](https://community.postman.com/t/jsonerror-response-without-response-body/42272/1 "2022-12-21T14:05:22Z")

</div>

I’m running into a really annoying problem that I can’t wrap my head around. I’m testing an API that returns empty response bodies when it uses a PUT or DELETE HTTP method and gets a 204 back (which is a successful operation). The response looks like this:

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

Now when I try to run the following test script:

 ![image](https://us1.discourse-cdn.com/getpostman/original/3X/2/1/21f73b2a3a0c5e83c1f9b07d5ce3dcb9411f2315.png)

I get the following error:  
 ![image](https://us1.discourse-cdn.com/getpostman/original/3X/1/9/192e8fa66cb4cea2580fe7cdac143a35174f7ec7.png)

I’m honestly just lost because I have no idea what’s going on. I’m not even directly calling on the response body in my test script. I’m only calling on the response code, which it does give, and the name of the request to get the field name (I have the field name in the request name).

Am I overlooking something obvious? Is the response messing with my script? I’m lost.

Full test script:

```auto
let devicePost = pm.request.method == "POST" && request.name.includes("Cash Device")
let devicePut = pm.request.method == "PUT" && request.name.includes("Cash Device")
let requestField = request.name.split("-").at(-1)
let responseMessage = pm.response.json().detail

if (devicePost || devicePut) {
    if (pm.response.code == 400) {
        try {
            if (responseMessage.includes("not found in enum") || responseMessage.includes("[ECMA 262 regex \"^\\d{4}(-\\d\\d(-\\d\\d(T\\d\\d:\\d\\d(:\\d\\d)?(\\.\\d+)?(([+-]\\d\\d:\\d\\d)|Z)?)?)?)?$\" does not match input string")) {
                // Informs that the field was correctly rejected
                pm.test("Field " + requestField + " was correctly rejected.")
            } else {
                pm.test("Field " + requestField + " was incorrectly rejected.")
            }
        } catch {
            pm.test("Testcase 4 Failed - Couldn't find strings in message,", () => { throw new Error("Message should have included one of the two expected strings") })
        }
    } else if (pm.response.code == 201) {
        pm.test("Testcase 4 Failed - Couldn't find strings in message,", () => { throw new Error("Field " + requestField + " should have been rejected.") })
    } else if (pm.response.code == 204) {
        pm.test("Testcase 4 Failed - Couldn't find strings in message,", () => { throw new Error("Field " + requestField + " should have been rejected.") })
    }
}

```

---

<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:** [December 21, 2022, 2:51pm UTC](https://community.postman.com/t/jsonerror-response-without-response-body/42272/2 "2022-12-21T14:51:35Z")

</div>

Are you trying to parse the response anywhere else in your code?

```auto
pm.response.json() 

```

console.log the requestField just before the test to ensure its actually hitting this bit of code.

---

<div class="post-metadata">

**Author:** ![rik.rodel1997](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/rik.rodel1997/32/14261_2.png) [@rik.rodel1997](https://community.postman.com/u/rik.rodel1997)\
**Post date:** [December 21, 2022, 6:40pm UTC](https://community.postman.com/t/jsonerror-response-without-response-body/42272/3 "2022-12-21T18:40:29Z")

</div>

Hi, I’ve updated my post with the full test script. I’m running the script from the overarching folder that holds the request. I don’t think I use the response somewhere else. I feel like I’m covering my bases with the if-else statements.

---

<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:** [December 22, 2022, 12:31pm UTC](https://community.postman.com/t/jsonerror-response-without-response-body/42272/4 "2022-12-22T12:31:18Z")

</div>

It’s this line that is causing the problem.

```auto
let responseMessage = pm.response.json().detail

```

This isn’t covered by your IF statements, so will always run. You can’t parse an empty response. Hence the error.

---

<div class="post-metadata">

**Author:** ![rik.rodel1997](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/rik.rodel1997/32/14261_2.png) [@rik.rodel1997](https://community.postman.com/u/rik.rodel1997)\
**Post date:** [December 22, 2022, 12:52pm UTC](https://community.postman.com/t/jsonerror-response-without-response-body/42272/5 "2022-12-22T12:52:56Z")

</div>

My inexperience is clearly showing here, that makes a lot of sense. Thanks a lot.

---

<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:** [November 17, 2023, 2:40pm UTC](https://community.postman.com/t/jsonerror-response-without-response-body/42272/6 "2023-11-17T14:40:29Z")

</div>

Closed on November 17, 2023, 2:40pm UTC

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