When handling client requests, the plugin will iterate through the nodes section of the configuration, requesting them in turn.
In the first request, it will send the complete method, query, header, and body of the client request, while in subsequent requests it will only send the last response with the POST method.
The successful response will be recorded in a temporary variable in last_resp
, and each request in the loop will get the response body of the previous request from last_resp.body
and overwrite the response body of the current request to last_resp
.
When the loop ends, the response body in the last_resp will be sent to the client as the final response.
-
Install the plugin to APISIX and add it to the plugin list.
-
Create some routes for testing
First is a route for demonstrating response body changes, I simply set it to convert all response bodies to uppercase and it is only used for demonstration purposes.
curl -X PUT 'http://127.0.0.1:9180/apisix/admin/routes/1' \
--header 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' \
--header 'Content-Type: application/json' \
--data-raw '{
"uri": "/transform",
"plugins": {
"serverless-pre-function":{
"phase": "access",
"functions": ["return function() local core = require(\"apisix.core\"); local body = core.request.get_body(); core.response.set_header(\"X-Test\", \"text\"); core.response.exit(200, string.upper(body)); end"]
}
},
"priority": 100
}'
The second one is the route that actually configures the pipeline-request plugin, which first gets a random uuid upstream from an httpbin, and later sends the response to the transform endpoint we just defined.
curl -XPUT 'http://127.0.0.1:9180/apisix/admin/routes/2' \
--header 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' \
--header 'Content-Type: application/json' \
--data-raw '{
"uri": "/test",
"plugins": {
"pipeline-request": {
"nodes": [
{
"uri": "https://httpbin.org/uuid",
"timeout": 2000
},
{
"uri": "http://127.0.0.1:9080/transform",
"timeout": 1000
}
]
}
},
"priority": 200
}'
- let's try it
Request #0:
curl http://httpbin.org/uuid
Response #0:
{
"uuid": "a0e8be56-2c2d-4b36-9755-86b100d3ee6b"
}
Request #1:
curl http://127.0.0.1:9080/test -i
Response #1: the uuid changes because it is random and we only need to focus on its characters
HTTP/1.1 200 OK
Content-Type: text/plain; charset=utf-8
Transfer-Encoding: chunked
Connection: keep-alive
Date: Fri, 16 Sep 2022 07:50:03 GMT
Server: APISIX/2.15.0
X-Test: text
Transfer-Encoding: chunked
Connection: keep-alive
{
"UUID": "8886D6D7-0778-4750-9292-42C4198CE79A"
}
Look at it, it's been rewritten in capital letters.