mswMock Service Worker
  1. Basics
  2. Request handler

Request handler

Request handler is a function that determines whether an outgoing request should be mocked, and specifies its mocked response.

Here's an example of a GET request handler:

1import { rest } from 'msw'
2
3// Matches any "GET /user" requests,
4// and responds using the `responseResolver` function.
5rest.get('/user', responseResolver)

Standard handlers

Mock Service Worker ships with multiple ready-to-use request handlers for working with popular API types. For example, you would use the rest handlers when mocking a REST API, and graphql request handlers for a GraphQL API.

List of handlers

rest/docs/api/rest
graphql/docs/api/graphql

Request matching

Request handlers are responsible for requests matching—a process of deciding whether a request should be mocked. Depending on the handler's type, request matching is performed differently. For example, rest handlers lookup matching methods and URLs, while graphql parse a request query string and expects a matching operation name.

Read more about which requests are being mocked in Request matching.