Mock Config logoMock config

Request body

Read and match REST request bodies in Mock Config.

Request bodies let you describe REST scenarios based on the payload the client sends. In Mock Config, request bodies are available on request.body inside your handler.

Read The Request Body

Use request.body to access the incoming payload:

import { rest } from 'mock-config-server';

type CreateUserBody = {
  name: string;
  role: 'admin' | 'developer';
};

const route = rest.post<{
  body: CreateUserBody;
}>('/users', ({ request }) => {
  const body = request.body;

  return {
    id: 1,
    name: body.name,
    role: body.role
  };
});

This is useful when the response depends on submitted form data, mutations, or command-like requests.

Body In Matchers

If you want to return different responses based on the request body, use body in the route matcher.

import { rest } from 'mock-config-server';

const routes = [
  rest.post(
    '/users',
    {
      ok: true,
      source: 'admin'
    },
    {
      match: {
        body: {
          role: 'admin'
        }
      }
    }
  )
];

This lets you keep one real route while describing multiple scenarios for different payload shapes.

Flat Body Matching

Mock Config also supports flat matching for nested body fields. That means you can match nested values using dot notation instead of repeating the full object structure.

import { rest } from 'mock-config-server';

const route = rest.post(
  '/users',
  {
    ok: true
  },
  {
    match: {
      body: {
        'profile.role': 'admin'
      }
    }
  }
);

Dot notation also works with arrays. Use the index to reach into a specific element:

import { rest } from 'mock-config-server';

const route = rest.post(
  '/users',
  {
    ok: true
  },
  {
    match: {
      body: {
        'users.0.role': 'admin'
      }
    }
  }
);

Flat matching is useful when:

  • the body is deeply nested
  • you want to match only a few nested fields

On this page