RequestMock.onRequestTo().respond Method

Specifies the mock response.

requestMock.onRequestTo().respond([body] [, statusCode] [, headers]) → this
Parameter Type Description Default
body (optional) Object | String | Function | Buffer A mocked response body. Pass an object for a JSON response, a string for an HTML response or a function to build a custom response. An empty HTML page is returned with the response.
statusCode (optional) Number The response status code. 200
headers (optional) Object Custom headers added to the response in the property-value form. The content-type header. If the header is not provided, it is set depending on the body parameter's type. If body is an object, the content-type header is set to application/json. If body has another type, the content-type header is set to text/html; charset=utf-8.
var mock = RequestMock()
    .respond({ data: 123 }) // a JSON response
    .respond('<html></html>') // an HTML response
    .respond(null, 204) // an empty response with a status code
    .respond('<html_markup>', 200, { // a response with custom headers
        'server': 'nginx/1.10.3'
    .respond(Buffer.from([0x62, 0x75, 0x66, 0x66, 0x65, 0x72])) // a response with binary data
    .respond((req, res) => { // a custom response
        res.headers['x-calculated-header'] = 'calculated-value';
        res.statusCode = '200';

        const parsedUrl = url.parse(req.path, true);

        res.setBody('calculated body' + parsedUrl.query['param']);

A Custom Response Function

var mock = RequestMock()
    .respond((req, res) => {
        // ...

A custom response function takes two parameters.

Parameter Type Description
req RequestOptions A request to be mocked.
res Object A mocked response.

Use information provided by the req parameter about the request to configure the response using the res parameter.

The res exposes the following members:

Property Type Description
headers Object The response headers.
statusCode Number The response status code.
Method Description
setBody(value) Sets the response body. Accepts a string as a parameter.

The response function can be synchronous or asynchronous:

var mock = RequestMock()
   .respond((req, res) => {
        res.setBody('<html><body><h1>This is a page</h1></body></html>');
    .respond(async (req, res) => {
        const body = await fetch('');


The RequestOptions object contains the request parameters.

Property Type Description
headers Object The request headers in the property-value form.
body Buffer The request body.
url String The URL of the resource.
protocol String The request protocol. Default: http:.
hostname String The domain name or IP address of the destination server. Default: localhost.
host String The domain name or IP address of the destination server, including the listening port. Default: localhost:80.
port Number The port of the remote server. Default: 80.
path String The request path. Should include query string if any. For example, '/index.html?page=12'. An exception is thrown when the request path contains illegal characters (e.g., spaces). Default: '/'.
method String The HTTP request method. Default: 'GET'.
credentials Object Credentials that were used for authentication in the current session with NTLM or Basic authentication. For HTTP Basic authentication, these are username and password. NTLM authentication additionally specifies workstation and domain. See HTTP Authentication.
proxy Object If a proxy is used, the property contains information about its host, hostname, port, proxyAuth, authHeader and bypassRules.
isAjax Boolean Specifies whether the request is an AJAX request (xhr or fetch).