JSONError response without response body

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:

Now when I try to run the following test script:

I get the following error:
image

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:

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.") })
    }
}

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

pm.response.json() 

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

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.

It’s this line that is causing the problem.

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.

1 Like

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

1 Like