.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>
Parameter | Type | Description |
---|---|---|
|
| The target URL (See url). |
|
| 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
Parameter | Type | Description | Default |
---|---|---|---|
| String / URL | The target URL of the request. The urlOpt parameter overrides this option. | - |
| String | The HTTP method. |
|
| Object | HTTP headers | { | - |
| Object | URLSearchParams | {[ | - |
| Any | Request body. | - |
| Number | Request timeout value (ms). |
|
| Boolean | Determines whether the response should include credentials such as cookies, authorization headers, and TLS client certificates. |
|
| AuthOptions | - | |
| ProxyOptions | - | |
| Boolean | Determines whether to display response body data without formatting. |
|
HTTP Authentication Credentials
Type: Object
Parameter | Type | Description | Default |
---|---|---|---|
| String | Username | - |
| String | Password | - |
Proxy Settings
Type: Object
Parameter | Type | Description | Default |
---|---|---|---|
| String | Proxy protocol | - |
| String | Response status text | - |
| Number / String | Port number | - |
| Object | - |
Authentication options
Type: Object
Parameter | Type | Description | Default |
---|---|---|---|
| String | Username | - |
| 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>
Parameter | Type | Description | Default |
---|---|---|---|
| Number | Response status | - |
| String | Response status text | - |
| Object | Response headers | - |
| IncomingMessage / Buffer / Object / String | - |