# `when` ( service in module `ngMock` )

## Description

Creates a new backend definition.

## Parameters

- `method – {string} –`

  HTTP method.

- `url – {string|RegExp} –`

  HTTP url.

- `data(optional) – {(string|RegExp)=} –`

  HTTP request body.

- `headers(optional) – {(Object|function(Object))=} –`

  HTTP headers or function that receives http header object and returns true if the headers match the current definition.

## Returns

`{requestHandler}` –

Returns an object with `respond` method that control how a matched request is handled.

- respond – `{function([status,] data[, headers])|function(function(method, url, data, headers)}` – The respond method takes a set of static data to be returned or a function that can return an array containing response status (number), response data (string) and response headers (Object).