Post processor scripts
Postprocessor script is a code snippet that is executed after the request has been sent. It is mainly used to assert whether the result returned by the request is correct or not, write the result data returned by the request to environment variables, etc.
Examples
Assert whether the result returned by the request is correct:
// pm.response.to.have Example
pm.test("Return status code 200", function() {
pm.response.to.have.status(200);
});
// pm.expect() Example
pm.test("The current environment is the production environment", function() {
pm.expect(pm.environment.get("env")).to.equal("production");
});
// response assertions Example
pm.test("No error in return result", function() {
pm.response.to.not.be.error;
pm.response.to.have.jsonBody("");
pm.response.to.not.have.jsonBody("error");
});
// pm.response.to.be* Example
pm.test("No error in return result", function() {
// assert that the status code is 200
pm.response.to.be.ok; // info, success, redirection, clientError, serverError, are other variants
// assert that the response has a valid JSON body
pm.response.to.be.withBody;
pm.response.to.be.json; // this assertion also checks if a body exists, so the above check is not needed
});
Write the returned data to environment variables:
// Get return data in JSON format
var jsonData = pm.response.json();
// Write the value of jsonData.token into an environment variable
pm.environment.set("token", jsonData.token);
:::highlight purple
Learn more about Assertion scripts.
:::
Last modified: 5 months ago