---
permalink: /helpers/REST
editLink: false
sidebar: auto
title: REST
---

<!-- Generated by documentation.js. Update this documentation by updating the source code. -->

## REST

**Extends Helper**

REST helper allows to send additional requests to the REST API during acceptance tests.
[Axios][1] library is used to perform requests.



## Configuration

Type: [object][4]

### Properties

-   `endpoint` **[string][3]** API base URL
-   `prettyPrintJson` **[boolean][6]?** pretty print json for response/request on console logs
-   `timeout` **[number][5]?** timeout for requests in milliseconds. 10000ms by default
-   `defaultHeaders` **[object][4]?** a list of default headers
-   `onRequest` **[function][7]?** a async function which can update request object.
-   `onResponse` **[function][7]?** a async function which can update response object.
-   `maxUploadFileSize` **[number][5]?** set the max content file size in MB when performing api calls.



## Example

```js
{
  helpers: {
    REST: {
      endpoint: 'http://site.com/api',
      prettyPrintJson: true,
      onRequest: (request) => {
      request.headers.auth = '123';
    }
  }
}
```

## Access From Helpers

Send REST requests by accessing `_executeRequest` method:

```js
this.helpers['REST']._executeRequest({
   url,
   data,
});
```

## Methods

### Parameters

-   `config`  

### _executeRequest

Executes axios request

#### Parameters

-   `request` **any** 

Returns **[Promise][2]&lt;any>** response

### _url

Generates url based on format sent (takes endpoint + url if latter lacks 'http')

#### Parameters

-   `url` **any** 

### amBearerAuthenticated

Adds a header for Bearer authentication

```js
// we use secret function to hide token from logs
I.amBearerAuthenticated(secret('heregoestoken'))
```

#### Parameters

-   `accessToken` **([string][3] | CodeceptJS.Secret)** Bearer access token

### haveRequestHeaders

Sets request headers for all requests of this test

#### Parameters

-   `headers` **[object][4]** headers list

### sendDeleteRequest

Sends DELETE request to API.

```js
I.sendDeleteRequest('/api/users/1');
```

#### Parameters

-   `url` **any** 
-   `headers` **[object][4]** the headers object to be sent. By default it is sent as an empty object 

Returns **[Promise][2]&lt;any>** response

### sendGetRequest

Send GET request to REST API

```js
I.sendGetRequest('/api/users.json');
```

#### Parameters

-   `url` **any** 
-   `headers` **[object][4]** the headers object to be sent. By default it is sent as an empty object 

Returns **[Promise][2]&lt;any>** response

### sendPatchRequest

Sends PATCH request to API.

```js
I.sendPatchRequest('/api/users.json', { "email": "user@user.com" });

// To mask the payload in logs
I.sendPatchRequest('/api/users.json', secret({ "email": "user@user.com" }));
```

#### Parameters

-   `url` **[string][3]** 
-   `payload` **any** the payload to be sent. By default it is sent as an empty object 
-   `headers` **[object][4]** the headers object to be sent. By default it is sent as an empty object 

Returns **[Promise][2]&lt;any>** response

### sendPostRequest

Sends POST request to API.

```js
I.sendPostRequest('/api/users.json', { "email": "user@user.com" });

// To mask the payload in logs
I.sendPostRequest('/api/users.json', secret({ "email": "user@user.com" }));
```

#### Parameters

-   `url` **any** 
-   `payload` **any** the payload to be sent. By default it is sent as an empty object 
-   `headers` **[object][4]** the headers object to be sent. By default it is sent as an empty object 

Returns **[Promise][2]&lt;any>** response

### sendPutRequest

Sends PUT request to API.

```js
I.sendPutRequest('/api/users.json', { "email": "user@user.com" });

// To mask the payload in logs
I.sendPutRequest('/api/users.json', secret({ "email": "user@user.com" }));
```

#### Parameters

-   `url` **[string][3]** 
-   `payload` **any** the payload to be sent. By default it is sent as an empty object 
-   `headers` **[object][4]** the headers object to be sent. By default it is sent as an empty object 

Returns **[Promise][2]&lt;any>** response

### setRequestTimeout

Set timeout for the request

```js
I.setRequestTimeout(10000); // In milliseconds
```

#### Parameters

-   `newTimeout` **[number][5]** timeout in milliseconds

: https://github.com/axios/axios

[2]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Promise

[3]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String

[4]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object

[5]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number

[6]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean

[7]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Statements/function
