# RESTHost - Before and After operation events

The RESTHost service has optional events that will execute before and after each operation. This allows you to override any details of the requests or responses, and it also allows you to add generic functionality that will execute for each request and response.

In order to expose these events in the Designer, navigate to the Show events area of the RESTHost service Properties and select the checkboxes next to Before operation and After operation. The RESTHost service in the Solution Explorer should now have the following events visible:

* OperationEvents\_AfterOperation
* OperationEvents\_BeforeOperation

***

## Before Operation

The OperationEvents\_BeforeOperation event will execute after the initial security validations and authentication event has succeeded.

In this event, the HTTPContext is available as an input.

You are then able to use these details to implement custom logic like the examples below:

### Logging Request Attempts

Request attempts and their associated metadata could be logged before each operation. This can be done in a number of ways, including logging attempts to a database or files.

In the below example, the TextFileWrite function is used to log requests and their associated IP information to a local file.

<div align="left"><figure><img src="https://3338214469-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F1nxDeDH8GVCXb0XelqGm%2Fuploads%2Fgit-blob-e9368a3e0cf70ec2cc5df03728401b7b53664f36%2Frest_operation_events_1.png?alt=media" alt="Logging Request Attempts"><figcaption></figcaption></figure></div>

<figure><img src="https://3338214469-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F1nxDeDH8GVCXb0XelqGm%2Fuploads%2Fgit-blob-14e4084bf432438cfbbb6a34b09668c6099853c3%2Frest_operation_events_2.png?alt=media" alt="Logging Request Attempts"><figcaption></figcaption></figure>

### Override Authenticate Event

You are also able to alter the HTTPContext of the output data of the OperationEvents\_Authenticate event. This allows you to override StatusCode, User, etc. values.

In order to correctly alter the outgoing HTTPContext, you first need to initiate the entire `$.Output.Data` by assigning its value to `$.Input.Data` using the SetValue function:

<div align="left"><figure><img src="https://3338214469-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F1nxDeDH8GVCXb0XelqGm%2Fuploads%2Fgit-blob-cf2ebfc7f2ecf19c563f7dde05a505ffbed288bd%2Frest_operation_events_3.png?alt=media" alt="Override Authenticate Event"><figcaption></figcaption></figure></div>

<div align="left"><figure><img src="https://3338214469-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F1nxDeDH8GVCXb0XelqGm%2Fuploads%2Fgit-blob-0580e8c08b9a41e58b38626ac6aac01fa29006fa%2Frest_operation_events_4.png?alt=media" alt="Override Authenticate Event"><figcaption></figcaption></figure></div>

Then you are able to assign specific values. In the example below, the `User.IsAuthenticated` value is overridden to `False`, which means the result of the OperationEvents\_Authenticate event will be overridden to `False`, and a 401 response will be returned, causing the request flow to cease.

<div align="left"><figure><img src="https://3338214469-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F1nxDeDH8GVCXb0XelqGm%2Fuploads%2Fgit-blob-448b690785d0ea8d4469f8646919074fdf055050%2Frest_operation_events_5.png?alt=media" alt="Override Authenticate Event"><figcaption></figcaption></figure></div>

### IP Whitelisting

Additional logic could be added to the OperationEvents\_BeforeOperation event, which validates if the incoming request originates from a “whitelist” of IP addresses. If the IP is allowed, then the flow can proceed. If the IP is invalid, then a (401) Unauthorized response could be returned by overriding the `$.Output.Data.User.IsAuthenticated` of the OperationEvents\_BeforeOperation event to `False`.

<div align="left"><figure><img src="https://3338214469-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F1nxDeDH8GVCXb0XelqGm%2Fuploads%2Fgit-blob-1789ff760d64a22ee5cada4fd77b9a42b05b4775%2Frest_operation_events_6.png?alt=media" alt="IP Whitelisting"><figcaption></figcaption></figure></div>

### Return Status Code

If you alter the `$.Output.Data.StatusCode` to anything, including 200, the response will be returned from the OperationEvents\_BeforeOperation event and the request flow will cease:

<div align="left"><figure><img src="https://3338214469-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F1nxDeDH8GVCXb0XelqGm%2Fuploads%2Fgit-blob-a331f960232f4f544c9bb6bd23ebd0e69036a977%2Frest_operation_events_7.png?alt=media" alt="Return Status Code"><figcaption></figcaption></figure></div>

## After Operation

The OperationEvents\_AfterOperation event will execute after the operation has executed.

You are able to overwrite or append the HTTPContext from the operation.

<div align="left"><figure><img src="https://3338214469-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F1nxDeDH8GVCXb0XelqGm%2Fuploads%2Fgit-blob-7c040478f6ea287397a7854eb256cb2b70b005e8%2Frest_operation_events_8.png?alt=media" alt="After Operation"><figcaption></figcaption></figure></div>

***

## Other Guides for Hosting a REST API

* [RESTHost Overview Guide](https://docs.linx.software/guides/rest-overview)
* [Get Started - Hello World](https://community.linx.software/t/resthost-hello-world/466)
* [Working with inputs](https://docs.linx.software/guides/resthost-working-with-inputs)
* [Handling responses](https://docs.linx.software/guides/rest-responses)
* [Securing your API](https://community.linx.software/t/resthost-guide-securing-your-api/462)
* [Deploying and common issues](https://community.linx.software/t/rest-webservices-debugging-deploying-and-common-issues/465)
* [Generating API documentation](https://community.linx.software/t/api-documentation-generation/472)
* [Sample solution: CRUD and file operations](https://community.linx.software/t/resthost-sample-solution-crud-and-file-operations/467)

***

## Sample

View our sample solution on [GitHub](https://github.com/linx-software/petstore-api).
