Configure Response Rewrite
The response-rewrite plugin allows you to modify the response from the upstream service before it reaches the client. This is useful for masking internal headers, changing status codes for specific conditions, or injecting content into the response body.
Prerequisites
- An API7 Enterprise instance is running.
- A Gateway Group is created and a Gateway instance is running.
- A token from the Dashboard.
Configure Response Body and Status Rewrite
You can use the plugin to replace the entire response body and change the HTTP status code.
- Admin API
- ADC
curl -k "https://localhost:7443/apisix/admin/services/response-rewrite-service?gateway_group_id={group_id}" -X PUT \
-H "X-API-KEY: ${API_KEY}" \
-H "Content-Type: application/json" \
-d '{
"name": "response-rewrite-service",
"upstream": {
"type": "roundrobin",
"scheme": "http",
"nodes": [
{
"host": "httpbin.org",
"port": 80,
"weight": 100
}
]
}
}'
curl -k "https://localhost:7443/apisix/admin/routes/response-rewrite-route?gateway_group_id={group_id}" -X PUT \
-H "X-API-KEY: ${API_KEY}" \
-H "Content-Type: application/json" \
-d '{
"name": "response-rewrite-route",
"paths": ["/get"],
"service_id": "response-rewrite-service",
"plugins": {
"response-rewrite": {
"status_code": 200,
"body": "{\"message\": \"Rewritten response\"}",
"headers": {
"set": {
"X-Rewritten-By": "API7-Enterprise",
"Content-Type": "application/json"
}
}
}
}
}'
services:
- name: response-rewrite-service
upstream:
nodes:
- host: httpbin.org
port: 80
weight: 1
routes:
- name: response-rewrite-route
uris:
- /get
plugins:
response-rewrite:
status_code: 200
body: "{\"message\": \"Rewritten response\"}"
headers:
set:
X-Rewritten-By: API7-Enterprise
Content-Type: application/json
adc sync -f adc.yaml
Configure Conditional Header Rewriting
You can also add, set, or remove headers based on specific conditions using Nginx variables.
- Admin API
- ADC
curl -k "https://localhost:7443/apisix/admin/services/conditional-response-rewrite-service?gateway_group_id={group_id}" -X PUT \
-H "X-API-KEY: ${API_KEY}" \
-H "Content-Type: application/json" \
-d '{
"name": "conditional-response-rewrite-service",
"upstream": {
"type": "roundrobin",
"scheme": "http",
"nodes": [
{
"host": "httpbin.org",
"port": 80,
"weight": 100
}
]
}
}'
curl -k "https://localhost:7443/apisix/admin/routes/conditional-response-rewrite-route?gateway_group_id={group_id}" -X PUT \
-H "X-API-KEY: ${API_KEY}" \
-H "Content-Type: application/json" \
-d '{
"name": "conditional-response-rewrite-route",
"paths": ["/get"],
"service_id": "conditional-response-rewrite-service",
"plugins": {
"response-rewrite": {
"headers": {
"add": [
"X-Server-ID: gateway-01"
],
"remove": [
"Server",
"X-Internal-Header"
]
},
"vars": [
["status", "==", 200]
]
}
}
}'
services:
- name: conditional-response-rewrite-service
upstream:
nodes:
- host: httpbin.org
port: 80
weight: 1
routes:
- name: conditional-response-rewrite-route
uris:
- /get
plugins:
response-rewrite:
headers:
add:
- "X-Server-ID: gateway-01"
remove:
- Server
- X-Internal-Header
vars:
- ["status", "==", 200]
adc sync -f adc.yaml
Validate the Configuration
Send a request to the route and check the response headers and body:
curl -i "http://127.0.0.1:9080/get"
The output should show the modified status code, the new headers, and the rewritten body content defined in the configuration.
If the route returns 404 immediately after you apply the configuration, wait a few seconds for the latest configuration to reach the gateway and retry.
Next Steps
- Configure CORS — allow cross-origin requests to your APIs.
- Inject Faults — test your application resilience.
- Configure Proxy Cache — improve performance by caching responses.