# Configure Response Rewrite

The [`response-rewrite`](https://docs.api7.ai/hub/response-rewrite.md) 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[​](#prerequisites "Direct link to Prerequisites")

* An API7 Enterprise instance is running.
* A Gateway Group is created and a Gateway instance is running.
* A [token from the Dashboard](https://docs.api7.ai/api7-gateway/reference/obtain-dashboard-token.md).

## Configure Response Body and Status Rewrite[​](#configure-response-body-and-status-rewrite "Direct link to 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={gateway_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={gateway_group_id}" -X PUT \

  -H "X-API-KEY: ${API_KEY}" \

  -H "Content-Type: application/json" \

  -d '{

    "name": "response-rewrite-route",

    "paths": ["/response-rewrite"],

    "service_id": "response-rewrite-service",

    "plugins": {

      "proxy-rewrite": {

        "uri": "/get"

      },

      "response-rewrite": {

        "status_code": 200,

        "body": "{\"message\": \"Rewritten response\"}",

        "headers": {

          "set": {

            "X-Rewritten-By": "API7-Enterprise",

            "Content-Type": "application/json"

          }

        }

      }

    }

  }'
```

adc.yaml

```
services:

  - name: response-rewrite-service

    upstream:

      nodes:

        - host: httpbin.org

          port: 80

          weight: 1

    routes:

      - name: response-rewrite-route

        uris:

          - /response-rewrite

        plugins:

          proxy-rewrite:

            uri: /get

          response-rewrite:

            status_code: 200

            body: "{\"message\": \"Rewritten response\"}"

            headers:

              set:

                X-Rewritten-By: API7-Enterprise

                Content-Type: application/json

    labels:

      docs-example: response-rewrite-service
```

Preview the changes owned by this example and confirm that the diff contains no unintended updates or deletions:

```
adc diff -f adc.yaml \

  --include-resource-type service \

  --label-selector docs-example=response-rewrite-service
```

Synchronize the reviewed configuration:

```
adc sync -f adc.yaml \

  --include-resource-type service \

  --label-selector docs-example=response-rewrite-service
```

## Configure Conditional Header Rewriting[​](#configure-conditional-header-rewriting "Direct link to 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={gateway_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={gateway_group_id}" -X PUT \

  -H "X-API-KEY: ${API_KEY}" \

  -H "Content-Type: application/json" \

  -d '{

    "name": "conditional-response-rewrite-route",

    "paths": ["/conditional-response-rewrite"],

    "service_id": "conditional-response-rewrite-service",

    "plugins": {

      "proxy-rewrite": {

        "uri": "/get"

      },

      "response-rewrite": {

        "headers": {

          "add": [

            "X-Server-ID: gateway-01"

          ],

          "remove": [

            "Server",

            "X-Internal-Header"

          ]

        },

        "vars": [

          ["status", "==", 200]

        ]

      }

    }

  }'
```

adc.yaml

```
services:

  - name: conditional-response-rewrite-service

    upstream:

      nodes:

        - host: httpbin.org

          port: 80

          weight: 1

    routes:

      - name: conditional-response-rewrite-route

        uris:

          - /conditional-response-rewrite

        plugins:

          proxy-rewrite:

            uri: /get

          response-rewrite:

            headers:

              add:

                - "X-Server-ID: gateway-01"

              remove:

                - Server

                - X-Internal-Header

            vars:

              - ["status", "==", 200]

    labels:

      docs-example: conditional-response-rewrite-service
```

Preview the changes owned by this example and confirm that the diff contains no unintended updates or deletions:

```
adc diff -f adc.yaml \

  --include-resource-type service \

  --label-selector docs-example=conditional-response-rewrite-service
```

Synchronize the reviewed configuration:

```
adc sync -f adc.yaml \

  --include-resource-type service \

  --label-selector docs-example=conditional-response-rewrite-service
```

## Validate the Configuration[​](#validate-the-configuration "Direct link to Validate the Configuration")

Check the first route's rewritten status, body, and `X-Rewritten-By` header:

```
curl -i "http://127.0.0.1:9080/response-rewrite"
```

Then check that the conditional route adds `X-Server-ID` and removes the configured headers:

```
curl -i "http://127.0.0.1:9080/conditional-response-rewrite"
```

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[​](#next-steps "Direct link to Next Steps")

* [Configure CORS](https://docs.api7.ai/api7-gateway/how-to-guides/traffic-management/cors.md) — allow cross-origin requests to your APIs.
* [Inject Faults](https://docs.api7.ai/api7-gateway/how-to-guides/traffic-management/fault-injection.md) — test your application resilience.
* [Configure Proxy Cache](https://docs.api7.ai/api7-gateway/how-to-guides/traffic-management/proxy-cache.md) — improve performance by caching responses.
