# Response

{% hint style="success" %}
This is the official documentation of Forest Admin Cloud.
{% endhint %}

After executing an action, the UI will display a notification message based on the HTTP status code received.

![](https://3681914749-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FwfMeGo4DK4ZOTYwApD1a%2Fuploads%2FQTL1MxZWCLvbGRJvY3ad%2Fimage.png?alt=media\&token=43065ccb-67ab-4602-a414-9224009f84bf)

***

{% hint style="info" %}
To make the code easier to read, all the code snippets below should be wrapped in the following code. Ensure you update the collection and action names as needed.

```typescript
import type { Agent } from '@forestadmin/forest-cloud';
import { Schema } from '../typings';

export default function customizeAgent(agent: Agent<Schema>) {
  agent.customizeCollection('contacts', (collection) => {
    collection.addAction('Ban user', {
      scope: 'Single',
      execute: async (context, resultBuilder) => {
        // Insert the code snippet here.
      },
    });
  });
}
```

{% endhint %}

### Simple response

The `resultBuilder` argument passed to the `execution` function enables you to customize the response of an action.

```typescript
return resultBuilder.success('Company is now live!');
```

![](https://3681914749-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FwfMeGo4DK4ZOTYwApD1a%2Fuploads%2FT89Temqq71oIzMH19n8P%2Fimage.png?alt=media\&token=57b5428b-df54-4f3c-b766-93460bb3b72a)

***

```typescript
return resultBuilder.error('Company was already live!');
```

![](https://3681914749-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FwfMeGo4DK4ZOTYwApD1a%2Fuploads%2F2Rp5pwfxXbRmmJftyKmD%2Fimage.png?alt=media\&token=18dae950-5b36-4de1-81d7-80335f8740bb)

***

### HTML response

Additionally, you can return an HTML response to present richer content.

<pre class="language-typescript"><code class="lang-typescript"><strong>return resultBuilder.success('Success', {
</strong>  html: `
    &#x3C;h2 style="color: #dc3545; text-align: center;">Contact Banned Successfully!&#x3C;/h2>
    &#x3C;p style="font-size: 16px; color: #333; text-align: center;">
      An automated email has been sent to the user detailing the reason for the ban.
      The user has a 30-day window to dispute the decision in order to prevent a
      permanent ban on the platform.
    &#x3C;/p>
  `,
});
</code></pre>

<figure><img src="https://3681914749-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FwfMeGo4DK4ZOTYwApD1a%2Fuploads%2F3EvyemTmEKu1JtfKtGeW%2FScreenshot%202024-02-20%20at%2015.11.58.png?alt=media&#x26;token=16ca39bc-5ec2-4332-92be-71c1ce36b4b6" alt=""><figcaption></figcaption></figure>

### File response

You can also provide a downloadable file as a response to a triggered action.

To use this feature, you have to include `generateFile: true` within the action's definition.

```typescript
collection.addAction('Ban user', {
  scope: 'Single',
  generateFile: true,
  execute: async (context, resultBuilder) => {
  // ...
```

#### **Inline content**

```typescript
return resultBuilder.file('The content of the file', 'filename.txt', 'text/plain');
```

#### **Buffer content**

```typescript
const buffer = Buffer.from('The content of the file');
return resultBuilder.file(buffer, 'filename.txt', 'text/plain');
```

#### **Stream content**

```typescript
const stream = fs.createReadStream('path/to/file');
return resultBuilder.file(stream, 'filename.txt', 'text/plain');
```

### Making a redirection

You can redirect to another location after triggering an action.

```typescript
return resultBuilder.redirectTo(
  '/MyProject/Production/Operations/data/contacts/index',
);
```

### Triggering a Webhook

Webhooks enable triggering actions in third-party applications or starting background tasks for users, using HTTP/HTTPS callbacks. Triggers occur in the user's browser, thus are subject to CORS restrictions.

```typescript
return resultBuilder.webhook(
  'https://mywebhookurl.someapp.com', // Webhook URL.
  'POST', // Webhook method (typically a POST).
  {}, // Webhook headers.
  {}, // Webhook body (only JSON is supported).
);
```

### Modifying HTTP Headers

It is possible to change the HTTP response headers by using the `setHeader()` function.

```typescript
return resultBuilder.setHeader('myHeaderName', 'myHeaderValue')
```

After calling `setHeader()`, you can call any response method previously described, such as:

```typescript
return resultBuilder
    .setHeader('myHeaderName', 'myHeaderValue')
    .success('Contact is now banned!').
```

{% hint style="info" %}
It's important to understand that the `webhook()` function and the `setHeader()` function serve different purposes. The former sends headers with the webhook call, while the latter modifies the headers of the HTTP response. These operations do not interfere with each other.
{% endhint %}
