0

I'm new to writing pre-request script and tests in Postman. So, I wonder if there's possible to make show the value from response body when the tests fail.

This is the example of the error response body and I want the test result to "FAIL" and show the value from "errorReason" in the test results tab.

{
    "errors": [
        {
            "errorCode": 401,
            "errorReason": "BALANCE_BELOW_ZERO"
        }
    ]
}
Yofi
  • 11
  • What did you already try? Please share your code. What concrete issues are you facing? Thanks for considering [How do I ask a good question](https://stackoverflow.com/help/how-to-ask\)? and [How to create a Minimal, Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example\). – Christian Baumann Mar 24 '22 at 08:59
  • Hi, so this is what I’ve tried so far. const response = pm.response.json(); const S = pm.variables.get("errorReason"); console.log(responseBody); pm.test("Gagal Melakukan Transfer", () => { if ("status" === 201) { pm.expect(status).to.equal(201) } else { pm.expect.fail(`ERROR REASON - ${S}`) } }); – Yofi Mar 24 '22 at 09:53

1 Answers1

-1

I using this way to get error message from api

async getSomething() {
    try {
      let response = await axios.get(
        `API/getSomething`
      );
      let data = response.data;
      htmlElememt.innerHTML = data 
      
    } catch (e) {
     // according to your error response 
      alert(e.response.errors.errorReason);
    }