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.
- Admin API
- ADC
- Ingress Controller
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
}
}
}'
Configure ADC to connect to the target backend:
- APISIX
- API7 Gateway
export ADC_BACKEND=apisix
export ADC_SERVER="http://127.0.0.1:9180"
export ADC_TOKEN="${ADMIN_API_KEY}"
export ADC_BACKEND=api7ee
export ADC_SERVER="https://{DASHBOARD_ADDR}"
export ADC_TOKEN="{DASHBOARD_TOKEN}"
export ADC_GATEWAY_GROUP="{GATEWAY_GROUP}"
Create the service and route:
services:
- name: echo-service
labels:
docs-example: echo
routes:
- name: echo-route
uris:
- /base64/dXBzdHJlYW0K
plugins:
echo:
body: "gateway response\n"
headers:
X-Echo-Mode: replace
upstream:
type: roundrobin
nodes:
- host: httpbin.org
port: 80
weight: 1
Preview the scoped changes:
adc diff -f adc.yaml \
--include-resource-type service \
--label-selector docs-example=echo
Review diff.yaml, then synchronize the labeled service:
adc sync -f adc.yaml \
--include-resource-type service \
--label-selector docs-example=echo
Set up the Ingress Controller and gateway in the aic namespace before continuing.
- Gateway API
- APISIX CRD
apiVersion: v1
kind: Service
metadata:
name: httpbin-external-domain
namespace: aic
spec:
type: ExternalName
externalName: httpbin.org
ports:
- port: 80
targetPort: 80
---
apiVersion: apisix.apache.org/v1alpha1
kind: PluginConfig
metadata:
name: echo-route
namespace: aic
spec:
plugins:
- name: echo
config:
body: "gateway response\n"
headers:
X-Echo-Mode: replace
---
apiVersion: gateway.networking.k8s.io/v1
kind: HTTPRoute
metadata:
name: echo-route
namespace: aic
spec:
parentRefs:
- name: apisix
rules:
- matches:
- path:
type: Exact
value: /base64/dXBzdHJlYW0K
filters:
- type: ExtensionRef
extensionRef:
group: apisix.apache.org
kind: PluginConfig
name: echo-route
backendRefs:
- name: httpbin-external-domain
port: 80
apiVersion: v1
kind: Service
metadata:
name: httpbin-external-domain
namespace: aic
spec:
type: ExternalName
externalName: httpbin.org
ports:
- port: 80
targetPort: 80
---
apiVersion: apisix.apache.org/v2
kind: ApisixPluginConfig
metadata:
name: echo-route
namespace: aic
spec:
ingressClassName: apisix
plugins:
- name: echo
enable: true
config:
body: "gateway response\n"
headers:
X-Echo-Mode: replace
---
apiVersion: apisix.apache.org/v2
kind: ApisixRoute
metadata:
name: echo-route
namespace: aic
spec:
ingressClassName: apisix
http:
- name: echo-route
match:
paths:
- /base64/dXBzdHJlYW0K
backends:
- serviceName: httpbin-external-domain
servicePort: 80
plugin_config_name: echo-route
Apply the configuration:
kubectl apply -f echo-ic.yaml
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:
- Admin API
- ADC
- Ingress Controller
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": "]"
}'
In adc.yaml, replace the existing plugin configuration:
plugins:
echo:
before_body: "["
after_body: "]"
Preview the updated service:
adc diff -f adc.yaml \
--include-resource-type service \
--label-selector docs-example=echo
Review diff.yaml, then synchronize the labeled service:
adc sync -f adc.yaml \
--include-resource-type service \
--label-selector docs-example=echo
Update the existing plugin configuration with the Kubernetes API used by the deployment:
- Gateway API
- APISIX CRD
apiVersion: apisix.apache.org/v1alpha1
kind: PluginConfig
metadata:
namespace: aic
name: echo-route
spec:
plugins:
- name: echo
config:
before_body: "["
after_body: "]"
apiVersion: apisix.apache.org/v2
kind: ApisixPluginConfig
metadata:
namespace: aic
name: echo-route
spec:
ingressClassName: apisix
plugins:
- name: echo
enable: true
config:
before_body: "["
after_body: "]"
Apply the updated plugin configuration:
kubectl apply -f echo-ic.yaml
Send the request again:
curl "http://127.0.0.1:9080/base64/dXBzdHJlYW0K"
The gateway preserves and wraps the upstream body:
[upstream
]