Kong Gateway - 22 基于网关服务的响应转换器(Response Transformer)

Configure a Service in Kong
[root@contoso ~]# curl -i -X POST \
--url http://localhost:8001/services/ \
--data 'name=country' \

--data 'url=http://contoso.com/v1/countries'

HTTP/1.1 201 Created
Date: Wed, 23 May 2018 07:25:37 GMT
Content-Type: application/json; charset=utf-8
Transfer-Encoding: chunked
Connection: keep-alive
Access-Control-Allow-Origin: *
Server: kong/0.13.1

{
    "host": "contoso.com", 
    "created_at": 1527031537, 
    "connect_timeout": 60000, 
    "id": "c5ddc5d7-f697-4038-a229-0be6dbdf7c21", 
    "protocol": "http", 
    "name": "country", 
    "read_timeout": 60000, 
    "port": 80, 
    "path": "/v1/countries", 
    "updated_at": 1527031537, 
    "retries": 5, 
    "write_timeout": 60000
}

Add a Route to expose the Service
URL Format http://localhost:8001/services/{name of servie}/routes
[root@contoso ~]# curl -i -X POST \
--url http://localhost:8001/services/country/routes \
--data 'paths[]=/v1/countries'

HTTP/1.1 201 Created
Date: Wed, 23 May 2018 07:26:41 GMT
Content-Type: application/json; charset=utf-8
Transfer-Encoding: chunked
Connection: keep-alive
Access-Control-Allow-Origin: *
Server: kong/0.13.1

{
    "created_at": 1527031601, 
    "strip_path": true, 
    "hosts": null, 
    "preserve_host": false, 
    "regex_priority": 0, 
    "updated_at": 1527031601, 
    "paths": [
        "/v1/countries"
    ], 
    "service": {
        "id": "c5ddc5d7-f697-4038-a229-0be6dbdf7c21"
    }, 
    "methods": null, 
    "protocols": [
        "http", 
        "https"
    ], 
    "id": "a9f498ca-6c47-4f4c-8622-4daf91a700b4"
}

/home/myth/www/bookstore/application/api/controller/v1/Country.php

public function getCountries() {
        return json([
            'RequestHeader' => [
                'h1' => request()->header('h1'), 'h2' => request()->header('h2'),
            ],
            'ResponseHeader' => [
                'h1' => response()->getHeader('h1'), 'h2' => response()->getHeader('h2'),
            ]
        ]);
        // $countries = Db::table('countries')->paginate(10,false,['query'=>request()->param('page')]);
        // return json($countries);
    }

Enabling the plugin for a Service

Add multiple headers by passing each header:value pair separately:
[root@contoso ~]# curl -i -X POST \
--url http://localhost:8001/services/country/plugins \
--data "name=response-transformer" \
--data "config.add.headers[1]=h1:v1" \
--data "config.add.headers[2]=h2:v1"
HTTP/1.1 201 Created
Date: Wed, 23 May 2018 08:10:51 GMT
Content-Type: application/json; charset=utf-8
Transfer-Encoding: chunked
Connection: keep-alive
Access-Control-Allow-Origin: *
Server: kong/0.13.1

{
    "created_at": 1527063051000, 
    "config": {
        "add": {
            "headers": [
                "h1:v1", 
                "h2:v1"
            ], 
            "json": { }
        }, 
        "append": {
            "headers": { }, 
            "json": { }
        }, 
        "remove": {
            "headers": { }, 
            "json": { }
        }, 
        "replace": {
            "headers": { }, 
            "json": { }
        }
    }, 
    "id": "bd567851-da9c-456e-87b3-51b8d16ee9e6", 
    "enabled": true, 
    "service_id": "c5ddc5d7-f697-4038-a229-0be6dbdf7c21", 
    "name": "response-transformer"
}

Enabling the plugin for a Service
Add multiple headers by passing each header:value pair separately:
URL Format  http://localhost:8001/routes/{route id}/plugins
[root@contoso ~]# curl -i -X POST \
--url http://localhost:8001/routes/a9f498ca-6c47-4f4c-8622-4daf91a700b4/plugins \
--data "name=response-transformer" \
--data "config.add.headers[1]=h1:v1" \
--data "config.add.headers[2]=h2:v1"
HTTP/1.1 201 Created
Date: Wed, 23 May 2018 08:14:52 GMT
Content-Type: application/json; charset=utf-8
Transfer-Encoding: chunked
Connection: keep-alive
Access-Control-Allow-Origin: *
Server: kong/0.13.1

{
    "created_at": 1527063292000, 
    "config": {
        "add": {
            "headers": [
                "h1:v1", 
                "h2:v1"
            ], 
            "json": { }
        }, 
        "append": {
            "headers": { }, 
            "json": { }
        }, 
        "remove": {
            "headers": { }, 
            "json": { }
        }, 
        "replace": {
            "headers": { }, 
            "json": { }
        }
    }, 
    "id": "63b88784-243a-4902-ac1d-a8148a0b00fd", 
    "enabled": true, 
    "route_id": "a9f498ca-6c47-4f4c-8622-4daf91a700b4", 
    "name": "response-transformer"
}

Testing to check if header parameter is valid

[root@contoso ~]# curl -i -X GET http://contoso.org:8000/v1/countries?page=5

HTTP/1.1 200 OK
Content-Type: application/json; charset=utf-8
Content-Length: 78
Connection: keep-alive
Date: Wed, 23 May 2018 10:33:54 GMT
Server: Apache/2.4.6 (CentOS) OpenSSL/1.0.2k-fips PHP/7.1.13
X-Powered-By: PHP/7.1.13
X-RateLimit-Limit-countries: countries=3
h1: v1
h2: v1
X-Kong-Upstream-Latency: 17
X-Kong-Proxy-Latency: 0
Via: kong/0.13.1

{"RequestHeader":{"h1":null,"h2":null},"ResponseHeader":{"h1":null,"h2":null}}


[root@contoso ~]# curl -i -X GET http://localhost:8001/services/country/plugins    // 根据name of service获取plugin id
[root@contoso ~]# curl -i -X GET http://localhost:8001/routes/a9f498ca-6c47-4f4c-8622-4daf91a700b4/plugins   // 根据route_id获取plugin id

[root@contoso ~]# curl -i -X DELETE \
--url http://localhost:8001/plugins/bd567851-da9c-456e-87b3-51b8d16ee9e6    // 根据plugin id删除它
[root@contoso ~]# curl -i -X DELETE \
--url http://localhost:8001/plugins/63b88784-243a-4902-ac1d-a8148a0b00fd    // 根据plugin id删除它


=================================================================

Add multiple headers by passing comma separated header:value pair:
[root@contoso ~]# curl -i -X POST \
--url http://localhost:8001/services/country/plugins \
--data "name=response-transformer" \
--data "config.add.headers=h1:v1,h2:v2"

HTTP/1.1 201 Created
Date: Wed, 23 May 2018 11:31:11 GMT
Content-Type: application/json; charset=utf-8
Transfer-Encoding: chunked
Connection: keep-alive
Access-Control-Allow-Origin: *
Server: kong/0.13.1

{
    "created_at": 1527075071000, 
    "config": {
        "add": {
            "headers": [
                "h1:v1", 
                "h2:v2"
            ], 
            "json": { }
        }, 
        "append": {
            "headers": { }, 
            "json": { }
        }, 
        "remove": {
            "headers": { }, 
            "json": { }
        }, 
        "replace": {
            "headers": { }, 
            "json": { }
        }
    }, 
    "id": "df206c9a-d47a-4951-b7e0-efea2965c1e3", 
    "enabled": true, 
    "service_id": "c5ddc5d7-f697-4038-a229-0be6dbdf7c21", 
    "name": "response-transformer"
}
Add multiple headers by passing comma separated header:value pair:
URL Format  http://localhost:8001/routes/{route id}/plugins
[root@contoso ~]# curl -i -X POST \
--url http://localhost:8001/routes/a9f498ca-6c47-4f4c-8622-4daf91a700b4/plugins \
--data "name=response-transformer" \
--data "config.add.headers=h1:v1,h2:v2"
HTTP/1.1 201 Created
Date: Wed, 23 May 2018 11:31:38 GMT
Content-Type: application/json; charset=utf-8
Transfer-Encoding: chunked
Connection: keep-alive
Access-Control-Allow-Origin: *
Server: kong/0.13.1

{
    "created_at": 1527075098000, 
    "config": {
        "add": {
            "headers": [
                "h1:v1", 
                "h2:v2"
            ], 
            "json": { }
        }, 
        "append": {
            "headers": { }, 
            "json": { }
        }, 
        "remove": {
            "headers": { }, 
            "json": { }
        }, 
        "replace": {
            "headers": { }, 
            "json": { }
        }
    }, 
    "id": "bbd83035-bc0d-41fe-9e80-a74c7d3960fa", 
    "enabled": true, 
    "route_id": "a9f498ca-6c47-4f4c-8622-4daf91a700b4", 
    "name": "response-transformer"
}
Testing to check if header parameter is valid
[root@contoso ~]# curl -i -X GET http://contoso.org:8000/v1/countries?page=5
HTTP/1.1 200 OK
Content-Type: application/json; charset=utf-8
Content-Length: 78
Connection: keep-alive
Date: Wed, 23 May 2018 11:37:52 GMT
Server: Apache/2.4.6 (CentOS) OpenSSL/1.0.2k-fips PHP/7.1.13
X-Powered-By: PHP/7.1.13
X-RateLimit-Limit-countries: countries=3
h1: v1
h2: v2
X-Kong-Upstream-Latency: 16
X-Kong-Proxy-Latency: 54
Via: kong/0.13.1

{"RequestHeader":{"h1":null,"h2":null},"ResponseHeader":{"h1":null,"h2":null}}

[root@contoso ~]# curl -i -X DELETE \
--url http://localhost:8001/plugins/df206c9a-d47a-4951-b7e0-efea2965c1e3    // 根据plugin id删除它
[root@contoso ~]# curl -i -X DELETE \
--url http://localhost:8001/plugins/bbd83035-bc0d-41fe-9e80-a74c7d3960fa    // 根据plugin id删除它


================================================================


下面的命令跟上面的终端命令格式稍有差别,把name of service和route_id与response-transformer
两条POST命令生成的数据删掉才可进行如下命令的实验,删除命令就是上面那两条指令

Add multiple headers by passing comma separated header:value pair:
[root@contoso ~]# curl -i -X POST \
--url http://localhost:8001/services/country/plugins \
--header 'content-type: application/json' \
--data '{"name": "response-transformer", "config": {"add": {"headers": ["h1:v2", "h2:v1"]}}}'


Add multiple headers passing config as JSON body:
URL Format  http://localhost:8001/routes/{route id}/plugins
[root@contoso ~]# curl -i -X POST \
--url http://localhost:8001/routes/a9f498ca-6c47-4f4c-8622-4daf91a700b4/plugins \
--header 'content-type: application/json' \
--data '{"name": "response-transformer", "config": {"add": {"headers": ["h1:v2", "h2:v1"]}}}'

详细演示过程省略跳过

================================================================

/home/myth/www/bookstore/application/api/controller/v1/Country.php

public function getCountries() {
        return json([
            'RequestHeader' => [
                'h1' => request()->header('h1'), 'h2' => request()->header('h2'),
                'p1' => request()->param('p1'), 'p2' => request()->param('p2'),
            ],
            'ResponseHeader' => [
                'h1' => response()->getHeader('h1'), 'h2' => response()->getHeader('h2'),
                'p1' => response()->getHeader('p1'), 'p2' => response()->getHeader('p2'),
            ]
        ]);
        // $countries = Db::table('countries')->paginate(10,false,['query'=>request()->param('page')]);
        // return json($countries);
    }

Add multiple headers by passing comma separated header:value pair:
[root@contoso ~]# curl -i -X POST \
--url http://localhost:8001/services/country/plugins \
--data "name=response-transformer" \
--data "config.add.json=p1:v1,p2:v2" \
--data "config.add.headers=h1:v1"

HTTP/1.1 201 Created
Date: Wed, 23 May 2018 12:34:14 GMT
Content-Type: application/json; charset=utf-8
Transfer-Encoding: chunked
Connection: keep-alive
Access-Control-Allow-Origin: *
Server: kong/0.13.1

{
    "created_at": 1527078854000, 
    "config": {
        "add": {
            "json": [
                "p1:v1", 
                "p2:v2"
            ], 
            "headers": [
                "h1:v1"
            ]
        }, 
        "append": {
            "headers": { }, 
            "json": { }
        }, 
        "remove": {
            "headers": { }, 
            "json": { }
        }, 
        "replace": {
            "headers": { }, 
            "json": { }
        }
    }, 
    "id": "6cd69a6e-c1b9-4b28-8007-1ddbbe6c7d36", 
    "enabled": true, 
    "service_id": "c5ddc5d7-f697-4038-a229-0be6dbdf7c21", 
    "name": "response-transformer"
}
Add a body property and a header:
URL Format  http://localhost:8001/routes/{route id}/plugins
[root@contoso ~]# curl -i -X POST \
--url http://localhost:8001/routes/a9f498ca-6c47-4f4c-8622-4daf91a700b4/plugins \
--data "name=response-transformer" \
--data "config.add.json=p1:v1,p2:v2" \
--data "config.add.headers=h1:v1"
HTTP/1.1 201 Created
Date: Wed, 23 May 2018 12:41:18 GMT
Content-Type: application/json; charset=utf-8
Transfer-Encoding: chunked
Connection: keep-alive
Access-Control-Allow-Origin: *
Server: kong/0.13.1

{
    "created_at": 1527079278000, 
    "config": {
        "add": {
            "json": [
                "p1:v1", 
                "p2:v2"
            ], 
            "headers": [
                "h1:v1"
            ]
        }, 
        "append": {
            "headers": { }, 
            "json": { }
        }, 
        "remove": {
            "headers": { }, 
            "json": { }
        }, 
        "replace": {
            "headers": { }, 
            "json": { }
        }
    }, 
    "id": "34cdbe78-9576-426c-addc-3c1ff1adeed1", 
    "enabled": true, 
    "route_id": "a9f498ca-6c47-4f4c-8622-4daf91a700b4", 
    "name": "response-transformer"
}
Testing to check if header parameter is valid
[root@contoso ~]# curl -i -X GET http://contoso.org:8000/v1/countries?page=5
HTTP/1.1 200 OK
Content-Type: application/json; charset=utf-8
Transfer-Encoding: chunked
Connection: keep-alive
Date: Wed, 23 May 2018 13:02:34 GMT
Server: Apache/2.4.6 (CentOS) OpenSSL/1.0.2k-fips PHP/7.1.13
X-Powered-By: PHP/7.1.13
X-RateLimit-Limit-countries: countries=3
h1: v1
X-Kong-Upstream-Latency: 16
X-Kong-Proxy-Latency: 0
Via: kong/0.13.1

{
    "RequestHeader": {
        "h2": null, 
        "h1": null, 
        "p2": null, 
        "p1": null
    }, 
    "p1": "v1", 
    "p2": "v2", 
    "ResponseHeader": {
        "h2": null, 
        "h1": null, 
        "p2": null, 
        "p1": null
    }
}

[root@contoso ~]# curl -i -X DELETE \
--url http://localhost:8001/plugins/6cd69a6e-c1b9-4b28-8007-1ddbbe6c7d36    // 根据plugin id删除它
[root@contoso ~]# curl -i -X DELETE \
--url http://localhost:8001/plugins/34cdbe78-9576-426c-addc-3c1ff1adeed1    // 根据plugin id删除它

======================================================================

/home/myth/www/bookstore/application/api/controller/v1/Country.php

public function getCountries() {
        return json([
            'RequestHeader' => [
                'h1' => request()->header('h1'), 'h2' => request()->header('h2'),
                'p1' => request()->param('p1'), 'p2' => request()->param('p2'),
            ],
            'p1'=>'v1','p2'=>'v2','p3'=>'v3',  // 请注意,接下来实验是把'p1'=>'v1'键-值对在输出到终端/浏览器前过滤掉
            'ResponseHeader' => [
                'h1' => response()->getHeader('h1'), 'h2' => response()->getHeader('h2'),
                'p1' => response()->getHeader('p1'), 'p2' => response()->getHeader('p2'),
            ]
        ]);
        // $countries = Db::table('countries')->paginate(10,false,['query'=>request()->param('page')]);
        // return json($countries);
    }
Append multiple headers and remove a body property:
[root@contoso ~]# curl -i -X POST \
--url http://localhost:8001/services/country/plugins \
--header 'content-type: application/json' \
--data '{"name": "response-transformer", "config": {"append": {"headers": ["h1:v2", "h2:v1"]}, "remove": {"json": ["p1"]}}}'
HTTP/1.1 201 Created
Date: Wed, 23 May 2018 14:20:50 GMT
Content-Type: application/json; charset=utf-8
Transfer-Encoding: chunked
Connection: keep-alive
Access-Control-Allow-Origin: *
Server: kong/0.13.1

{
    "created_at": 1527085250000, 
    "config": {
        "replace": {
            "headers": { }, 
            "json": { }
        }, 
        "append": {
            "headers": [
                "h1:v2", 
                "h2:v1"
            ], 
            "json": { }
        }, 
        "remove": {
            "json": [
                "p1"
            ], 
            "headers": { }
        }, 
        "add": {
            "headers": { }, 
            "json": { }
        }
    }, 
    "id": "85de6ea3-474d-40ef-b108-6b5283960998", 
    "enabled": true, 
    "service_id": "c5ddc5d7-f697-4038-a229-0be6dbdf7c21", 
    "name": "response-transformer"
}
Append multiple headers and remove a body property:
URL Format  http://localhost:8001/routes/{route id}/plugins
[root@contoso ~]# curl -i -X POST \
--url http://localhost:8001/routes/a9f498ca-6c47-4f4c-8622-4daf91a700b4/plugins \
--header 'content-type: application/json' \

--data '{"name": "response-transformer", "config": {"append": {"headers": ["h1:v2", "h2:v1"]}, "remove": {"json": ["p1"]}}}'

HTTP/1.1 201 Created
Date: Wed, 23 May 2018 14:22:18 GMT
Content-Type: application/json; charset=utf-8
Transfer-Encoding: chunked
Connection: keep-alive
Access-Control-Allow-Origin: *
Server: kong/0.13.1

{
    "created_at": 1527085338000, 
    "config": {
        "replace": {
            "headers": { }, 
            "json": { }
        }, 
        "append": {
            "headers": [
                "h1:v2", 
                "h2:v1"
            ], 
            "json": { }
        }, 
        "remove": {
            "json": [
                "p1"
            ], 
            "headers": { }
        }, 
        "add": {
            "headers": { }, 
            "json": { }
        }
    }, 
    "id": "58864f35-9f31-45d0-aff6-bcdbcee6eedf", 
    "enabled": true, 
    "route_id": "a9f498ca-6c47-4f4c-8622-4daf91a700b4", 
    "name": "response-transformer"
}
Testing to check if header parameter and body property is valid
[root@contoso ~]# curl -i -X GET http://contoso.org:8000/v1/countries?page=5
HTTP/1.1 200 OK
Content-Type: application/json; charset=utf-8
Transfer-Encoding: chunked
Connection: keep-alive
Date: Wed, 23 May 2018 14:49:24 GMT
Server: Apache/2.4.6 (CentOS) OpenSSL/1.0.2k-fips PHP/7.1.13
X-Powered-By: PHP/7.1.13
X-RateLimit-Limit-countries: countries=3
h1: v2
h2: v1
X-Kong-Upstream-Latency: 18
X-Kong-Proxy-Latency: 0
Via: kong/0.13.1

{
    "p2": "v2", 
    "p3": "v3", 
    "RequestHeader": {
        "h2": null, 
        "h1": null, 
        "p2": null, 
        "p1": null
    }, 
    "ResponseHeader": {
        "h2": null, 
        "h1": null, 
        "p2": null, 
        "p1": null
    }
}
相关文章
相关标签/搜索