Capture ResponseCode in the pre request

Hello everybody!
from the pre request I am executing a service and I want to capture the statusCode, I had tried this pm.test (“BadParams”, function () {pm.response.to.have.status (400);});

but this code only works for the test tab, for the pre request it gives me an error. they know if it is possible to do …

Hey @bj-nicoo :wave:

There might be some confusion around what “Pre-request script” is so let me clarify on this first :slightly_smiling_face:

It works as following order:
pre-request script → sending request → Tests
So pr-request script gets executed before the request is sent, so pre-request script does not know what the response would be (or what the response code would be).

I hope this makes sense but please do feel free to reach out if you have further questions :smiley:

hi @taehoshino !
Thank you very much for taking the time to resolve my question.
Maybe I explained it wrong.
After researching I was able to figure out my code and it already does what I need.
I needed → pre request-> (get-> Response-> capture statusCode-> show Status)

pm.sendRequest({

method: 'GET',

url: pm.environment.get("url") + '/v1/oauth/accesstoken?grant_type=client_credential',

header: { 'Authorization': pm.environment.get("authorizationCB") },

body: {

    mode: 'raw',

    raw: JSON.stringify({ key: "this is json" })

}

} , function (err, res) {

pm.test("Params-Value-incorrecto: 400", function () {

    pm.expect(err).to.not.be.ok;

    pm.expect(res).to.have.property('code', 400);

});

});

I share the command, I hope it is useful.
goal hug!

1 Like