Insomnia logo
  • Documentation
  • Get Started for Free
    • Introduction to Insomnia
    • Install Insomnia
    • Send Your First Request
    • Import and Export Data
    • Environment Variables
    • Global Environments
    • Insomnia Accounts
    • Forgotten Passphrase
    • Managing E2EE (End-to-End Encryption)
    • Organizations
    • Enable Enterprise membership
    • Configuring EE SSO
    • Integrating Insomnia Enterprise with Okta SAML 2.0
    • Integrating Insomnia Enterprise with Okta OpenID Connect
    • Integrating Insomnia Enterprise with Microsoft Azure/Entra ID SAML 2.0
    • Insomnia Whitelisting Guide for Enterprise Users
    • Transfer enterprise organizations and license
    • Configuring SCIM
    • Multiple Owners
    • Manage Domains
    • Invite Controls
    • Storage Controls
    • Session Report
    • Insomnia Subscriptions
    • Insomnia Subscription Management
    • Scratch Pad Tutorial
    • Requests
    • Responses
    • Request Collections
    • Request Timeouts
    • Chaining Requests
    • Post CSV Data
    • SOAP Requests
    • gRPC
    • WebSocket Support
    • Get Started with Documents
    • Design Documents
    • Linting
    • GraphQL for OpenAPI
    • Migrate from Designer
    • Unit Testing
    • Stress Testing
    • Insomnia Storage Options Guide
    • Sync with Insomnia Cloud
    • Sync with Git
    • Key Security Features
    • Security Standards
    • Signup and Authentication
    • Analytics Collected
    • End to End Data Encryption
    • Software Bill of Materials
    • Verifying Build Provenance for Signed Insomnia Binaries
    • Authentication
    • Client Certificates
    • Collection Runner
    • Generate Code Snippet
    • Cookie Management
    • Encoding
    • GraphQL Queries
    • Run in Insomnia Button
    • Key Maps
    • Proxy
    • Folder-level settings
    • Introduction to Plugins
    • Context Object Reference
    • Template Tags
    • Hooks and Actions
    • Custom Themes
    • AI Runner
    • FAQ
    • Application Data
    • SSL Validation
    • Password Recovery
    • Introduction to Inso CLI
    • Install Inso CLI
    • CLI Command Reference
      • inso run test
      • inso run collection
      • inso lint spec
      • inso export spec
      • inso script
      • Using Custom Linting with Inso CLI
    • Configuration
    • Inso CLI on Docker
    • Software Bill of Materials
    • Verifying Signatures for Signed Inso CLI Images
    • Verifying Inso CLI Build Provenance
      • Verifying Build Provenance for Signed Inso CLI Images
      • Verifying Build Provenance for Signed Inso CLI Binaries
    • Continuous Integration
    • Insomnia Pre-request Script Overview
    • Insomnia After-Response Script Overview
    • Secret Environment Variables
    • External Vault Integration (Enterprise feature)
    • Insomnia API Mocking Overview
    • Enterprise Login Report

Insomnia After-Response Script Overview

Starting with the release of Insomnia 9.3.0, the concept of after-response scripts (also known as post-request scripts or test scripts in other applications) has been introduced. These scripts are executed following the completion of a request.

In after-response scripts, you can perform similar scripting activities as you would in pre-request scripts. For more details on pre-request scripts, see the documentation here.

These scripts can be utilized for:

  • Performing tests and assertions on the response.
  • Storing certain parts of the response into environment variables.
  • Any other script you would apply in pre-request scripts.

For tests and assertions you can use insomnia.test and insomnia.expect functions:

insomnia.test('Check if status is 200', () => {
  insomnia.expect(insomnia.response.code).to.eql(200);
});

Here are some examples insomnia.expect calls you can make:

insomnia.expect(200).to.eql(200);
insomnia.expect('uname').to.be.a('string');
insomnia.expect('a').to.have.lengthOf(1);
insomnia.expect('xxx_customer_id_yyy').to.include("customer_id");
insomnia.expect(201).to.be.oneOf([201,202]);
insomnia.expect(199).to.be.below(200);

// Testing objects
insomnia.expect({a: 1, b: 2}).to.have.all.keys('a', 'b');
insomnia.expect({a: 1, b: 2}).to.have.any.keys('a', 'b');
insomnia.expect({a: 1, b: 2}).to.not.have.any.keys('c', 'd');
insomnia.expect({a: 1}).to.have.property('a');
insomnia.expect({a: 1, b: 2}).to.be.an('object').that.has.all.keys('a', 'b');

You can also access various response attributes:

const status = insomnia.response.status;
const responseTime = insomnia.response.responseTime;
const jsonBody = insomnia.response.json();
const textBody = insomnia.response.text();
const header = insomnia.response.headers.find(header => header.key === 'Content-Type');
const cookies = insomnia.response.cookies.toObject();

Example of storing response body parts into the environment:

insomnia.environment.set("whole_response", insomnia.response.json());

// Storing a specific field
const jsonBody = insomnia.response.json();
insomnia.environment.set("specific_field", jsonBody.specific_field);

Similar to pre-request scripts, after-response scripts allow for actions such as importing libraries, setting delays, and sending requests at the end of the script.

const atob = require('atob');

await new Promise((resolve) => setTimeout(resolve, 1000));

const resp = await new Promise((resolve, reject) => {
  insomnia.sendRequest(
    'https://mock.insomnia.rest',
    (err, resp) => {
      err ? reject(err) : resolve(resp);
    }
  );
});

Migrating from Postman post-request scripts

After-response scripts exported from Postman should also work when imported into Insomnia.

There are some differences to be aware about:

  • Top level awaits are allowed.
  • Global environment insomnia.globals and iteration data insomnia.iterationData are not supported yet.
  • CollectionVariables is mapped to baseEnvironment in Insomnia.
  • Deprecated postman interfaces are not supported yet, such as postman.setEnvironmentVariable.

If you notice any incompatibility issues, please report these by create a new issue on GitHub.

Edit this page
Report an issue
    COMPANY
  • Insomnia
  • Blog
  • Changelog
  • Pricing
  • Careers
    PRODUCTS
  • Insomnia
  • Inso (CLI)
    RESOURCES
  • Sign In
  • Documentation
  • Support
    LEGAL
  • Privacy Policy
  • Terms & Conditions
© Kong Inc. 2021