Request Rewrite
Prepare environment
Please refer to API7 EE Introduction to complete the environment preparation.
Create route with proxy rewrite
Create an API, this time we don't need to open the plugin.
Set a special uri
that is not /anything
.
Next we set two things,
- Rewrites path by replacing prefix, rewriting
/otherapi
to/anything
. - Adds a header and removes a client's header.
X-Test = JustForPoC
will be addedX-Delete
form client will be deleted
Test
Request to /otherapi
and rewrite
curl 127.0.0.1/otherapi -i -H "Host: example.com"
HTTP/1.1 200 OK
Content-Type: application/json
Content-Length: 423
Connection: keep-alive
Date: Mon, 13 Mar 2023 05:50:01 GMT
Access-Control-Allow-Origin: *
Access-Control-Allow-Credentials: true
Server: APISIX/2.13.2304
{
"args": {},
"data": "",
"files": {},
"form": {},
"headers": {
"Accept": "*/*",
"Host": "example.com",
"User-Agent": "curl/7.81.0",
"X-Amzn-Trace-Id": "Root=1-640eb989-36c729174cc2d500586bb7c5",
"X-Forwarded-Host": "example.com",
"X-Test": "JustForPoC" # X-Test header added
},
"json": null,
"method": "GET",
"origin": "172.17.0.1, 146.190.80.65",
"url": "http://example.com/anything" # /otherapi uri rewrited
}
Request and delete client's header
curl 127.0.0.1/otherapi -i -H "Host: example.com" -H "X-Delete: WillBeDeleted"
HTTP/1.1 200 OK
Content-Type: application/json
Content-Length: 423
Connection: keep-alive
Date: Mon, 13 Mar 2023 05:52:18 GMT
Access-Control-Allow-Origin: *
Access-Control-Allow-Credentials: true
Server: APISIX/2.13.2304
{
"args": {},
"data": "",
"files": {},
"form": {},
"headers": { # X-Delete header deleted
"Accept": "*/*",
"Host": "example.com",
"User-Agent": "curl/7.81.0",
"X-Amzn-Trace-Id": "Root=1-640eba12-193711166c97a91b01ba30b9",
"X-Forwarded-Host": "example.com",
"X-Test": "JustForPoC"
},
"json": null,
"method": "GET",
"origin": "172.17.0.1, 146.190.80.65",
"url": "http://example.com/anything"
}