.request()

The t.request TestController method executes an HTTP request and returns the server’s response.

async request(url [, options]) -> Promise<ResponseOptions>

async request(options) -> Promise<ResponseOptions>

async request[methodName](options) -> Promise<ResponseOptions>
ParameterTypeDescription

url 

String / URL

The target URL (See url).

options (optional)

RequestOptions

See options.

URL

Every HTTP request needs a target URL. The target URL can be either absolute or relative. If you specify a relative URL, TestCafe calculates the absolute URL in relation to the current page.

There are two ways to specify the target URL.

  • You can pass the URL string directly to the request method:

    await t.request(`http://localhost:3000/api/data`);
    
  • Alternatively, use the url option:

    await t.request({
        url: 'http://localhost:3000/api/data'
    });
    

If you define both, the first argument takes priority:

await t.request(`http://localhost:3000/api/1`, {url: 'http://localhost:3000/api/2'}); // sends a request to /api/1

Options

Type: Object

ParameterTypeDescriptionDefault

url 

String / URL

The target URL of the request. The urlOpt parameter overrides this option.

-

method 

String

The HTTP method.

GET

headers 

Object

HTTP headers | {key: value}

-

params 

Object

URLSearchParams | {[key: string]: string | number | boolean}

-

body 

Any

Request body.

-

timeout 

Number

Request timeout value (ms).

25000

withCredentials 

Boolean

Determines whether the response should include credentials such as cookies, authorization headers, and TLS client certificates.

false

auth 

AuthOptions

HTTP authentication credentials.

-

proxy 

ProxyOptions

Proxy settings

-

rawResponse 

Boolean

Determines whether to display response body data without formatting.

false

HTTP Authentication Credentials

Type: Object

ParameterTypeDescriptionDefault

username 

String

Username

-

password 

String

Password

-

Proxy Settings

Type: Object

ParameterTypeDescriptionDefault

protocol 

String

Proxy protocol

-

host 

String

Response status text

-

port 

Number / String

Port number

-

auth 

Object

Authentication options

-

Authentication options

Type: Object

ParameterTypeDescriptionDefault

username 

String

Username

-

password 

String

Password

-

Methods

If your request uses one of the following common HTTP methods, you can omit the method option:

  • Get
  • Delete
  • Head
  • Post
  • Put
  • Patch

Instead, append the name of the HTTP method to the request method itself.

The following two code snippets yield identical results:

await t.request({
    url: 'http://localhost:3000/api/data',
    method: 'head'
});
await t.request.head({
    url: 'http://localhost:3000/api/data'
});

Return value

Type: Promise<ResponseOptions>

ParameterTypeDescriptionDefault

status 

Number

Response status

-

statusText 

String

Response status text

-

headers 

Object

Response headers

-

body 

IncomingMessage / Buffer / Object / String

Response body

-