115 Star 698 Fork 166

GVPiresty / Apache APISIX

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
response-rewrite.md 2.93 KB
一键复制 编辑 原始数据 按行查看 历史

English

response-rewrite

该插件支持修改上游服务返回的 body 和 header 信息。

使用场景: 1、可以设置 Access-Control-Allow-* 等 header 信息,来实现 CORS (跨域资源共享)的功能。 2、另外也可以通过配置 status_code 和 header 里面的 Location 来实现重定向,当然如果只是需要重定向功能,最好使用 redirect 插件。

配置参数

名字 可选 说明
status_code 可选 修改上游返回状态码,默认保留原始响应代码。
body 可选 修改上游返回的 body 内容,如果设置了新内容,header 里面的 content-length 字段也会被去掉
body_base64 可选 布尔类型,描述 body 字段是否需要 base64 解码之后再返回给客户端,用在某些图片和 Protobuffer 场景
headers 可选 返回给客户端的 headers,这里可以设置多个。头信息如果存在将重写,不存在则添加。想要删除某个 header 的话,把对应的值设置为空字符串即可

示例

开启插件

下面是一个示例,在指定的 route 上开启了 response rewrite 插件:

curl http://127.0.0.1:9080/apisix/admin/routes/1  -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
{
    "methods": ["GET"],
    "uri": "/test/index.html",
    "plugins": {
        "response-rewrite": {
            "body": "{\"code\":\"ok\",\"message\":\"new json body\"}",
            "headers": {
                "X-Server-id": 3,
                "X-Server-status": "on"
            }
        }
    },
    "upstream": {
        "type": "roundrobin",
        "nodes": {
            "127.0.0.1:80": 1
        }
    }
}'

测试插件

基于上述配置进行测试:

curl -X GET -i  http://127.0.0.1:9080/test/index.html

如果看到返回的头部信息和内容都被修改了,即表示 response rewrite 插件生效了。

HTTP/1.1 200 OK
Date: Sat, 16 Nov 2019 09:15:12 GMT
Transfer-Encoding: chunked
Connection: keep-alive
X-Server-id: 3
X-Server-status: on

{"code":"ok","message":"new json body"}
Lua
1
https://gitee.com/iresty/apisix.git
git@gitee.com:iresty/apisix.git
iresty
apisix
Apache APISIX
v1.5

搜索帮助