Skip to main content

Version: latest

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.

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"
}
}
}
}
}'

Configure Conditional Header Rewriting

You can also add, set, or remove headers based on specific conditions using Nginx variables.

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]
]
}
}
}'

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

API7.ai Logo

The digital world is connected by APIs,
API7.ai exists to make APIs more efficient, reliable, and secure.

Sign up for API7 newsletter

Product

API7 Gateway

SOC2 Type IIISO 27001HIPAAGDPRRed Herring

Copyright © APISEVEN PTE. LTD 2019 – 2026. Apache, Apache APISIX, APISIX, and associated open source project names are trademarks of the Apache Software Foundation