---
schema_version: "1"
id: 75881769c6d57910e33c35ac8a851dd7c662e62974294b3bb7fcb3b3d1556271
title: Stopping Request and Response Execution
collection: mulesoft
version: current
locale: en-us
portal_url: https://docs.salesforce.com/mulesoft/current/en-us/pdk/1.9/policies-pdk-configure-features-stop.md
canonical_url: https://docs.mulesoft.com/pdk/1.9/policies-pdk-configure-features-stop
source_site: docs.mulesoft.com
content_type: documentation
content_hash: 2d5d047367028db5528cea9a6aaf6f2660199a3cf45ef6b27b31f60adf3d7268
description: MuleSoft Documentation Site
release: "1.9"
latest: false
---
---
product: Omni Gateway Policy Development Kit (PDK)
version: 1.9
---

# Stopping Request and Response Execution

> For the full documentation index, see: https://docs.mulesoft.com/llms.txt

Omni Gateway Policy Development Kit (PDK) enables you to send early responses during the request and
response execution to terminate the execution flow.

To intercept and stop a request or response, return a `Response` object from your `on_request` or
`on_response` wrapped functions:

```Rust
Response::new(401)
    .with_headers(vec![("WWW-Authenticate".to_string(), "Bearer realm=\"oauth2\"".to_string())])
    .with_body(r#"{ "error": "token was not present"}"#)
```

To return the \`Response\`object, see:

- [Stop Request Execution](#stop-request-execution)

- [Stop Response Execution](#stop-response-execution)

## Stop Request Execution

Block or allow requests to reach the upstream service using a `Flow`. A `Flow` in an `enum` value
with two possible values:

- `Continue`: Defines an object that is forwarded to the response.

- `Break`: Aborts the request and returns the provided response.

Create a `Flow` as follows:

```Rust
async fn request_filter(request_state: RequestState) -> Flow<()> {
    let header_state = request_state.into_headers_state().await;
    let handler = header_state.handler();
    if handler.header("authorization").is_some() {
        Flow::Continue(())
    } else {
        Flow::Break(Response::new(401)
        .with_headers(vec![("WWW-Authenticate".to_string(), "Bearer realm=\"oauth2\"".to_string())])
        .with_body(r#"{ "error": "token was not present"}"#))
    }
}
```

> [!IMPORTANT] Due to the streaming nature of `proxy wasm`, it is possible for an early request to
> partially reach the upstream service while awaiting the `headers_state.into_body_state` function.
> Avoid this is by not awaiting the body. However, this is not possible for all policies. Configure
> your upstream to ignore partial requests if the policy must read the body.
>
> To avoid early responses reaching the client, see
> [Sharing Data Between Requests and Responses](https://docs.mulesoft.com/pdk/1.9/policies-pdk-configure-features-share-data).

## Stop Response Execution

To send an early response during the `on_response` phase, return a `Response` object using the
`send_response` method of the `ResponseHeadersState`. You can’t send an early response from the body
state:

```Rust
pub fn send_response(self, response: Response) {
```

The `send_response` method enables you to add a body to responses that don’t already have one.
Sending an early response using the `send_response` method terminates the execution flow. You can’t
perform any other operation after sending an early response:

```Rust
async fn response_filter(state: ResponseState) {
   let state = state.into_headers_state().await;

   state.send_response(
       Response::new(200)
           .with_headers(vec![("some".to_string(), "200".to_string())])
           .with_body("Some"),
   );
}
```

## See Also

- [Implementing Your Custom Policy Features in Rust](https://docs.mulesoft.com/pdk/1.9/policies-pdk-configure-features)
