Skip to main content

Echo

The echo plugin replaces or wraps an upstream response body and can add response headers. It is useful for testing routes and returning controlled responses while still exercising upstream request handling.

Examples​

The examples show how to replace an upstream response body with configured content and how to add content before and after the upstream body.

Replace the Response Body​

Configure a route that replaces the upstream response body and adds a response header.

Create the route:

curl "http://127.0.0.1:9180/apisix/admin/routes/echo-route" -X PUT \
-H "X-API-KEY: ${ADMIN_API_KEY}" \
-d '{
"uri": "/base64/dXBzdHJlYW0K",
"plugins": {
"echo": {
"body": "gateway response\n",
"headers": {
"X-Echo-Mode": "replace"
}
}
},
"upstream": {
"type": "roundrobin",
"nodes": {
"httpbin.org:80": 1
}
}
}'

Send a request to the route:

curl --include "http://127.0.0.1:9080/base64/dXBzdHJlYW0K"

The gateway returns the configured header and replacement body:

HTTP/1.1 200 OK
X-Echo-Mode: replace
...
gateway response

Wrap the Upstream Body​

Update the same route to preserve the upstream body and add content before and after it:

Update the route plugin:

curl "http://127.0.0.1:9180/apisix/admin/routes/echo-route/plugins/echo" -X PATCH \
-H "X-API-KEY: ${ADMIN_API_KEY}" \
-H "Content-Type: application/json" \
-d '{
"before_body": "[",
"after_body": "]"
}'

Send the request again:

curl "http://127.0.0.1:9080/base64/dXBzdHJlYW0K"

The gateway preserves and wraps the upstream body:

[upstream
]