---
schema_version: "1"
id: 7da7c2a8b23a1858ac29ab7a6409ea3b2b465bd04d41bb290adc69fd30d1acd4
title: Reading and Writing Request Headers and Bodies with Stop Iteration
collection: mulesoft
version: current
locale: en-us
portal_url: https://docs.salesforce.com/mulesoft/current/en-us/pdk/1.8/policies-pdk-configure-features-headers-stop.md
canonical_url: https://docs.mulesoft.com/pdk/1.8/policies-pdk-configure-features-headers-stop
source_site: docs.mulesoft.com
content_type: documentation
content_hash: ff4a8e9f8fbf15f1000e7d757988c39ae5a5b7941b2c781025b33fca38a22435
description: MuleSoft Documentation Site
release: "1.8"
latest: false
---
---
product: Omni Gateway Policy Development Kit (PDK)
version: 1.8
---

# Reading and Writing Request Headers and Bodies with Stop Iteration

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

Use the Omni Gateway Policy Development Kit (PDK) `enable_stop_iteration` feature to simultaneously
read and modify headers and body content.

PDK Policies using stop iteration only supports payload bodies of up to 1MB.

> [!NOTE] Only Omni Gateway version 1.12.0 or later support stop interation.

## Enable Stop Iteration in your Cargo.toml

To enable this feature, add `enable_stop_iteration` to your `Cargo.toml` file dependencies:

```toml
[dependencies]
pdk = { version = "1.8.0", features = ["enable_stop_iteration"] }
```

## Read and Write Request Headers

After enabling the feature, use `into_headers_body_state()` to transition from the initial request
or response state to the combined headers-body state:

```Rust
use pdk::hl::*;

async fn request_filter(request_state: RequestState) -> Flow<()> {
   // Transition to headers-body state
   let state = request_state.into_headers_body_state().await;

   // Access both headers and body through the unified handler
   state.handler().set_header("x-custom-header", "value");

   if state.contains_body() {
       let _ = state.handler().set_body("modified body".as_bytes());
   }

   Flow::Continue(())
}
```

The `into_headers_body_state()` method returns a `RequestHeadersBodyState` for request flows or
`ResponseHeadersBodyState` for response flows. Both states provide a unified `handler()` that
implements both `HeadersHandler` and `BodyHandler` traits:

```Rust
pub trait Handler {
    fn headers(&self) -> Vec<(String, String)>;
    fn header(&self, name: &str) -> Option<String>;
    fn add_header(&self, name: &str, value: &str);
    fn set_header(&self, name: &str, value: &str);
    fn set_headers(&self, headers: Vec<(&str, &str)>);
    fn remove_header(&self, name: &str);
    fn body(&self) -> Vec<u8>;
    fn set_body(&self, body: &[u8]) -> Result<(), BodyError>;
    fr contains_body(&self) -> Bool;
}
```

## Handle Requests Without Body

When a request or response doesn’t contain a body, the headers-body state handler gracefully handles
the absence:

```Rust
async fn request_filter(request_state: RequestState) -> Flow<()> {
   let state = request_state.into_headers_body_state().await;

   // Check if body exists
   if state.contains_body() {
       let body = state.handler().body();
       // Process body
   }

   // Attempting to set body on a request without body returns an error
   if let Err(BodyError::BodyNotSent) = state.handler().set_body("data".as_bytes()) {
       // Handle case where body cannot be set (e.g., GET request)
       state.handler().set_header("x-data", "data");
   }

   Flow::Continue(())
}
```

## Stop Iteration Configuration Examples

PDK provides the
[Stop Iteration Example policy](https://github.com/mulesoft/pdk-custom-policy-examples/tree/1.8.0/stop-iteration)
to demonstrate how to use the headers-body unified state in Rust code.

Within the example policy, see these code sections for additional configuration details:

- [Transition to headers-body state and modify both headers and body in request filter](https://github.com/mulesoft/pdk-custom-policy-examples/tree/1.8.0/stop-iteration/src/lib.rs#L10-L40)

- [Use headers-body state in response filters](https://github.com/mulesoft/pdk-custom-policy-examples/tree/1.8.0/stop-iteration/src/lib.rs#L42-L61)

- [Handle body modification with error handling](https://github.com/mulesoft/pdk-custom-policy-examples/tree/1.8.0/stop-iteration/src/lib.rs#L25-L37)
