This plugin adds and sets HTTP headers on client requests as they pass through the proxy. It demonstrates the distinction between adding a header (which appends to any existing value) and setting/replacing a header (which overwrites any existing value). Use this plugin when you need to inject metadata, routing hints, or default values into upstream requests. It operates during the request headers processing phase.
- The proxy receives an HTTP request from a client and invokes the plugin's
on_http_request_headerscallback. - The plugin adds a
Message: helloheader. If the request already contains aMessageheader, the new value is appended (comma-separated), resulting inMessage: <original>, hello. - The plugin sets the
Welcomeheader towarm. If the request already contains aWelcomeheader, the existing value is replaced entirely. - The plugin returns
Action::Continue, allowing the (now modified) request to proceed to the upstream server.
- Appending vs replacing headers: This plugin demonstrates the distinction between "adding" a header (appending with a comma to existing values if present) and "setting" or "replacing" a header (completely overwriting it).
No configuration required.
Build the plugin for any supported language from the plugins/ directory:
# Rust
bazelisk build //samples/add_request_header:plugin_rust.wasm
# C++
bazelisk build //samples/add_request_header:plugin_cpp.wasm
# Go
bazelisk build //samples/add_request_header:plugin_go.wasmRun the unit tests defined in tests.textpb:
# Using Docker (recommended)
docker run -it -v $(pwd):/mnt \
us-docker.pkg.dev/service-extensions-samples/plugins/wasm-tester:main \
--proto /mnt/samples/add_request_header/tests.textpb \
--plugin /mnt/bazel-bin/samples/add_request_header/plugin_rust.wasm
# Using Bazel (all languages)
bazelisk test --test_output=all //samples/add_request_header:testsDerived from tests.textpb:
| Scenario | Description |
|---|---|
| AddsRequestHeader | Injects the new headers when they do not previously exist on the request. |
| UpdatesRequestHeader | Appends to the existing Message header and overwrites the existing Welcome header. |
| RequestAndResponse | Verifies that the modifications occur only on the request phase. |
| CaseInsensitiveUpdates | Properly updates existing headers regardless of their original case. |