Request Transforming
Sometimes you may need to transform the API requests before forwarding them to the backend services, such as adding/replacing some request headers, modify the request URI, and change the HTTP method.
This guide tells you how to use the Proxy Rewrite plugin to do the request transforming.
Prepare the Environment
Deploy Apache APISIX
Please refer to How to Deploy Apache APISIX to learn how to deploy Apache APISIX and connect it to API7 Cloud. In this guide, we'll deploy an Apache APISIX instance on Docker.
Create Service and Route
We'll create a service with the following details in this guide.
- The service name is
transform-app
. - The path prefix is
/v1
. - The HTTP Host is
rewrite.httpbin.org
. - The upstream URL is
https://httpbin.org
.
Besides, we'll create a route inside the transform-app
Service.
- The route name is
anything
. - The path is
/anything
(prefix match), and strip the path prefix. - It accepts all HTTP methods.
If you don't know how to configure a service and route, please refer to the Getting Started guides first
Test Request Transforming
Now let's test the request transforming. We'll show you the following transforming scenarios.
- Rewrite HTTP Method
- Rewrite Request URI
- Modify Request Headers
You can also combine the above transforming cases according to your actual situation.
Rewrite HTTP Method
Let's create the Proxy Rewrite plugin (on the anything
route) in which we rewrite the HTTP method to GET
.
Now let's send a request to verify it.
curl http://127.0.0.1:9080/v1/anything -H 'Host: rewrite.httpbin.org' -s -XPOST
{
"args": {},
"data": "",
"files": {},
"form": {},
"headers": {
"Accept": "*/*",
"Host": "rewrite.httpbin.org",
"User-Agent": "curl/7.76.1",
"X-Amzn-Trace-Id": "Root=1-62b55b92-3bee7a141e72e1187ae696a7",
"X-Forwarded-Host": "rewrite.httpbin.org"
},
"json": null,
"method": "GET",
"origin": "172.17.0.1, 61.241.66.251",
"url": "https://rewrite.httpbin.org/anything"
}
The original HTTP method is POST,
and it's rewritten to GET
(the method
field).
Rewrite Request URI
Replace URI
Assume that we want to access the /json
route when we access /v1/anything
. In such a case, let's update the
Proxy Rewrite plugin just like below:
Now let's send a request and see the response.
curl http://127.0.0.1:9080/v1/anything -H 'Host: rewrite.httpbin.org'
{
"slideshow": {
"author": "Yours Truly",
"date": "date of publication",
"slides": [
{
"title": "Wake up to WonderWidgets!",
"type": "all"
},
{
"items": [
"Why <em>WonderWidgets</em> are great",
"Who <em>buys</em> WonderWidgets"
],
"title": "Overview",
"type": "all"
}
],
"title": "Sample Slide Show"
}
}
As you can see, we got the JSON response, which means Apache APISIX accessed the /json
route.
Regex Replace URI
Sometimes we want to replace the URI only if the current request URI meets some conditions. Under such a circumstance, we can use the regex replace, filling the regex pattern and the replacement pattern.
The New URI can use the capture group so that it can inherit some URI components from the original URI.
Let's send a request which URI contains the base64 code of "Hello, API7 Cloud!"
. You can calculate the base64 code by running:
echo 'Hello, API7 Cloud!' | base64
curl http://127.0.0.1:9080/v1/anything/SGVsbG8sIEFQSTcgQ2xvdWQhCg== -H 'Host: rewrite.httpbin.org' -s
And the response body will be:
Hello, API7 Cloud!
Modify Request Headers
You may also want to operate (add, replace, delete) the HTTP request headers in the API Gateway for some business needs. Let's update the Proxy Rewrite plugin as per the rules below:
- Add or replace the
Accept-Encoding
header, the value isdeflate
; - Add or replace the
X-Proxy-Component
header, value isApache APISIX
; - Delete the
User-Agent
header;
curl http://127.0.0.1:9080/v1/anything -H 'Host: rewrite.httpbin.org' -H 'Accept-Encoding: gzip'
{
"args": {},
"data": "",
"files": {},
"form": {},
"headers": {
"Accept": "*/*",
"Accept-Encoding": "deflate",
"Host": "rewrite.httpbin.org",
"X-Amzn-Trace-Id": "Root=1-62b5652d-2ca3b15750386d2a2d7f679e",
"X-Forwarded-Host": "rewrite.httpbin.org",
"X-Proxy-Component": "Apache APISIX"
},
"json": null,
"method": "GET",
"origin": "172.17.0.1, 61.241.66.251",
"url": "https://rewrite.httpbin.org/anything"
}
As you can see, Apache APISIX adds the X-Proxy-Component
header, rewrites the Accept-Encoding
(original value is gzip
), and removes the
User-Agent
header (curl always adds the User-Agent
).