---
description: Descriptive full API of a Context.
---

# Context

{% hint style="warning" %}
A part of the context method is linked to the default [ResponseHandler](../core/concepts/responsehandler) provided by Idylle. Specific method are design to adapt HTTP code when responding to an HTTP Request. For a better understanding feel free to check the [HTTP Code Meaning](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status).
{% endhint %}

## Properties

### data : Object { String: Any }

### params : Object { String: String }

### token: String

### user: Object { String: Any }

### session: Object { String: Any }

### files: Array\<File>

### args: Object { String : Any }

### criteria: Criteria

{% hint style="info" %}
Note the properties will depends on the `CriteriaBuilder` used by the Action. It can be overridden globally during the [initialization phase](../../core/the-flows#customizing-the-flow) or for a specific action by overriding the criteriaBuilder property.
{% endhint %}

## Methods

### .error(code, message, ...args) -> Promise (rejected)

`.error(code, message, ...args) -> Promise (rejected)` create a rejection with a [ContextError.](contexterror)

```javascript
...
execute: context => {
    return User
        .findById(context.params.id)
        .then(u => u || context.error(404, 'user_not_found', { id: context.params.id }))
}
...
```

### .ok(resource) -> resource

`context.ok(resource) -> resource` changes the state of the context to tell the default `ResponseHandler` that the response should be a **200 OK**.&#x20;

```javascript
...
        execute: context => {
            return getCompetitionDetails()
                .then(c => c || context.error(400, 'invalid competition'))
                .then(context.ok);
...
```

### .created(resource) -> resource

`context.created(resource) -> resource` changes the state of the context to tell the default `ResponseHandler` that the response should be a **201 Created** and let the response body empty.

{% hint style="info" %}
Note that if the provided resource has a property `uri` it will be written in the `location` response header.
{% endhint %}

```javascript
...
        execute: context => {
            return getCompetitionDetails()
                .then(c => c || context.error(400, 'invalid competition'))
                .then(context.created);
...
```

### .partial(resource) -> resource

`context.partial(resource) -> resource` changes the state of the context to tell the default  `ResponseHandler` that the response should be a **206 Partial**.&#x20;

```javascript
... 
        execute: context => {
            return getCompetitionList()
                .then(c => c.truncated ? context.partial(c) : c);
...
```

### .redirect(code=302, url) -> void

`context.redirect(code, url) -> void` changes the state of the context to tell the default  `ResponseHandler` that the response should be a **301** **Moved Permanently**. Generally used when a deprecated method is still called from an old client.

```javascript
... 
        execute: context => {
            return context.redirect(null, '/competitions/list');
...
```

### .stream(path | buffer) -> resource

`context.stream(path | buffer) -> void` changes the state of the context to tell the default  `ResponseHandler` that a `File` or `Buffer` has to be downloaded from the given path.&#x20;

```javascript
... 
        execute: context => {
            return getCompetitionList()
                .then(c => c.truncated ? context.partial(c) : c);
...
```

### .noContent(resource) -> resource

`context.noContent(resource) -> resource` changes the state of the context to tell the default  `ResponseHandler` that the response should be a **204 No Content** and let the response body empty.

```javascript
... 
        execute: context => {
            return getCompetitionList()
                .then(c => c.truncated ? context.partial(c) : c);
...
```

