Testing the body of a status 400 request

My question:
I want to test whether or not the request I’m sending:

  1. returns a status code 400
  2. contains the desired error message in the return body

I am looking for a solution for 2).

Details (like screenshots):

  1. is easily solved, but I cannot find a solution to validate point 2.

I’ve already tried:
pm.test(“Body contains string”,() => {
pm.expect(pm.response.text()).to.include(‘“The chosen growth model depends on the field "unit dimension - area" of the unit to be filled.”’);

})

pm.test(“Body is correct”, function () {
pm.response.to.have.body(‘{“errors”:{“unit”:[{“code”:“productiongroup.unitGrowthModelDependsOnUnitArea”,“message”:“The chosen growth model depends on the field "unit dimension - area" of the unit to be filled.”}]},“type”:“RFC 7231: Hypertext Transfer Protocol (HTTP/1.1): Semantics and Content Request”,“status”:400,“traceId”:“00-8dc5cb52d7a586e6e908ca43857dadaa-da58410cb7341006-00”}’);
});

This would be the assertion you need for the message text in that response example:

pm.expect(pm.response.json().errors.unit[0].message).to.eql('The message text')

Put that line in the pm.test() function.

You can reduce the length of the reference and capture that as a variable but that’s up to you. :grin:

1 Like

Worked like a charm, thank you!

1 Like