diff --git a/en/docs/api-management-overview/create-api-using-crs.md b/en/docs/api-management-overview/create-api-using-crs.md index 3ee4f3c4a..1b71ad50f 100644 --- a/en/docs/api-management-overview/create-api-using-crs.md +++ b/en/docs/api-management-overview/create-api-using-crs.md @@ -1,3 +1,3 @@ We support API management through a set of Custom Resources (CRs) that adhere to Kubernetes Gateway Specification. See [APK Kubernetes CRD Catalog]({{base_path}}/en/latest/catalogs/kubernetes-crds) for more information. -Additionally, our REST API flow can generate a set of CRs based on the API definitions provided through OpenAPI Specifications, Service Description Languages (SDLs), and APK configuration files. This flow is designed to streamline the API development process and reduce the manual effort required to create basic API related CRs. Follow steps mentions in [Generate APK Configuration File]({{base_path}}/en/latest/api-management-overview/tools-for-api-development/#option-2-generate-k8s-custom-resources-using-config-generator-tool-and-deploy-the-api-using-kubernetes-client) to generate APK configuration files. +Additionally, our REST API flow can generate a set of CRs based on the API definitions provided through OpenAPI Specifications, Service Description Languages (SDLs), and APK configuration files. This flow is designed to streamline the API development process and reduce the manual effort required to create basic API related CRs. Follow the steps mentioned in [Generate APK Configuration File]({{base_path}}/en/latest/api-management-overview/tools-for-api-development/#option-2-generate-k8s-custom-resources-using-config-generator-tool-and-deploy-the-api-using-kubernetes-client) to generate APK configuration files. diff --git a/en/docs/assets/img/api-management/api-policies/webhook-site-request-mirroring.png b/en/docs/assets/img/api-management/api-policies/webhook-site-request-mirroring.png new file mode 100644 index 000000000..b6535f143 Binary files /dev/null and b/en/docs/assets/img/api-management/api-policies/webhook-site-request-mirroring.png differ diff --git a/en/docs/create-api/create-and-attach-api-policies/cors/enable-cors-via-rest-api.md b/en/docs/create-api/create-and-attach-api-policies/cors/enable-cors-via-rest-api.md index b35a8a913..bd846823c 100644 --- a/en/docs/create-api/create-and-attach-api-policies/cors/enable-cors-via-rest-api.md +++ b/en/docs/create-api/create-and-attach-api-policies/cors/enable-cors-via-rest-api.md @@ -112,7 +112,8 @@ corsConfiguration: - "*" ``` - Sample APK configuration content after the modification is shown below. +Sample APK configuration content after the modification is shown below. + ``` name: "EmployeeServiceAPI" basePath: "/test" diff --git a/en/docs/create-api/create-and-attach-api-policies/header-modifier-filters/header-modifier-via-crs.md b/en/docs/create-api/create-and-attach-api-policies/header-modifier-filters/header-modifier-via-crs.md new file mode 100644 index 000000000..74acf84ee --- /dev/null +++ b/en/docs/create-api/create-and-attach-api-policies/header-modifier-filters/header-modifier-via-crs.md @@ -0,0 +1,191 @@ +# Header Modification via CRs + +This functionality enables the addition, modification, and removal of request and response headers for APIs. By customizing headers, you can enhance the control and flexibility of API interactions, ensuring that both incoming requests and outgoing responses meet specific requirements. + +### Step 1 - Get the CRs for the relevant API configuration + +Here, you can follow the steps in [Develop and Deploy a REST API via CRs](../../create-and-deploy-apis/rest/create-rest-api-using-crs.md) documentation and create the CRs to deploy an API from scratch. + +Alternatively, you can generate the CRs for a given apk-conf file using the steps as detailed in [this section]({{base_path}}/en/latest/api-management-overview/tools-for-api-development/#option-2-generate-k8s-custom-resources-using-config-generator-tool-and-deploy-the-api-using-kubernetes-client) + +### Step 2 - Add the header modification filters to the HTTPRoute CR + +Header modification can be done using an HTTPRoute filter as follows. + +``` + - type: "RequestHeaderModifier" + requestHeaderModifier: + set: + - name: "Set-Request-Header" + value: "Set-Value" + add: + - name: "Add-Request-Header" + value: "Added-Value" + remove: + - "Remove-Request-Header" +``` + +This filter does the following modifications to the request headers. + +1. Update the header named "Set-Request-Header" with the value "Set-Value". +2. Adds a header named "Add-Request-Header" with the value "Added-Value". +3. Removes the header named "Remove-Request-Header". + +!!! Note + - By replacing the type with "ResponseHeaderModifier", the modifications can be done to the response. + - Both RequestHeaderModifier and ResponseHeaderModifier can be added to the same rule. + +An HTTPRoute with the header modifiers is given below. + +``` +--- +apiVersion: "gateway.networking.k8s.io/v1beta1" +kind: "HTTPRoute" +metadata: + name: "production-httproute" +spec: + hostnames: + - "default.gw.wso2.com" + rules: + - matches: + - path: + type: "RegularExpression" + value: "/employee" + method: "GET" + filters: + - type: "URLRewrite" + urlRewrite: + path: + type: "ReplaceFullPath" + replaceFullPath: "/employee" + - type: "RequestHeaderModifier" + requestHeaderModifier: + set: + - name: "Set-Request-Header" + value: "Test-Value" + add: + - name: "Test-Request-Header" + value: "Test-Value" + remove: + - "Remove-Header" + backendRefs: + - group: "dp.wso2.com" + kind: "Backend" + name: "api-backend" +``` + +Sample configurations for each of them have been provided under the [Sample Configurations](#sample-configurations) section. + +### Step 3 - Deploy the API in APK +You can deploy the API using the following command. Replace with the correct namespace. +``` +kubectl apply -f . -n +``` + +### Sample Configurations + +#### Request Header Modification + +##### 1. Add Request Header + +``` +rules: + - matches: + - path: + type: "RegularExpression" + value: "/employee" + method: "GET" + filters: + - type: "RequestHeaderModifier" + requestHeaderModifier: + add: + - name: "Add-Request-Header" + value: "Added-Value" +``` + +##### 2. Update Request Header + +``` +rules: + - matches: + - path: + type: "RegularExpression" + value: "/employee" + method: "GET" + filters: + - type: "RequestHeaderModifier" + requestHeaderModifier: + add: + - name: "Set-Request-Header" + value: "Set-Value" +``` + +##### 3. Remove Request Header + +``` +rules: + - matches: + - path: + type: "RegularExpression" + value: "/employee" + method: "GET" + filters: + - type: "RequestHeaderModifier" + requestHeaderModifier: + add: + - name: "Add-Request-Header" + value: "Added-Value" +``` + +#### Response Header Modification + +##### 1. Add Request Header + +``` +rules: + - matches: + - path: + type: "RegularExpression" + value: "/employee" + method: "GET" + filters: + - type: "ResponseHeaderModifier" + requestHeaderModifier: + add: + - name: "Add-Request-Header" + value: "Added-Value" +``` + +##### 2. Update Request Header + +``` +rules: + - matches: + - path: + type: "RegularExpression" + value: "/employee" + method: "GET" + filters: + - type: "ResponseHeaderModifier" + requestHeaderModifier: + add: + - name: "Set-Request-Header" + value: "Set-Value" +``` + +##### 3. Remove Response Header + +``` +rules: + - matches: + - path: + type: "RegularExpression" + value: "/employee" + method: "GET" + filters: + - type: "ResponseHeaderModifier" + requestHeaderModifier: + add: + - name: "Add-Request-Header" + value: "Added-Value" +``` diff --git a/en/docs/create-api/create-and-attach-api-policies/header-modifier-filters/header-modifier-via-rest-api.md b/en/docs/create-api/create-and-attach-api-policies/header-modifier-filters/header-modifier-via-rest-api.md new file mode 100644 index 000000000..1ca45c97b --- /dev/null +++ b/en/docs/create-api/create-and-attach-api-policies/header-modifier-filters/header-modifier-via-rest-api.md @@ -0,0 +1,290 @@ +# Header Modification via APK Conf + +This functionality enables the addition, modification, and removal of request and response headers for APIs. By customizing headers, you can enhance the control and flexibility of API interactions, ensuring that both incoming requests and outgoing responses meet specific requirements. + +### Step 1 - Get the API configuration + +Here, you can follow the steps in [Create an API](../../../get-started/quick-start-guide.md) documentation and save this content into a file named `EmployeeService.apk-conf`. You can use this apk-conf file for the rest of this guide. + +### Step 2 - Add the header modification policy to the apk-conf file + +A sample header modification configuration is given below. + +``` + - target: "/employee" + verb: "GET" + secured: false + scopes: [] + operationPolicies: + request: + - policyName: AddHeader + policyVersion: v1 + parameters: + headerName: "Test-Request-Header" + headerValue: "Test-Value" +``` + +This policy adds a header with the name "Test-Request-Header" and value "Test-Value" to the request sent to this particular path. + +The complete apk-conf file with this configuration is given below. + +``` +id: "header-modifier-api" +name: "EmployeeServiceAPI" +basePath: "/employees" +version: "3.14" +type: "REST" +defaultVersion: false +endpointConfigurations: + production: + endpoint: "https://httpbin.org/anything" +operations: +- target: "/employee" + verb: "GET" + secured: false + scopes: [] + operationPolicies: + request: + - policyName: AddHeader + policyVersion: v1 + parameters: + headerName: "Test-Request-Header" + headerValue: "Test-Value" +- target: "/employee" + verb: "POST" + secured: true + scopes: [] +- target: "/employee/{employeeId}" + verb: "PUT" + secured: true + scopes: [] +- target: "/employee/{employeeId}" + verb: "DELETE" + secured: true + scopes: [] +``` + +Similarly, you can do the following to both request and response headers. +1. Add headers +2. Update existing headers +3. Remove headers + +Sample configurations for each of them have been provided under the [Sample Configurations](#sample-configurations) section. + +### Step 3 - Deploy the API in APK + +Refer to the [Deploy the API in APK](../../../get-started/quick-start-guide.md#deploy-the-api-in-apk) to deploy the API using APK configuration. + +### Step 4 - Generate an access token + +Follow the [Generate Access Token](../../../develop-and-deploy-api/security/generate-access-token.md) documentation to generate an access token. + +### Step 5 - Invoke the API + +You can invoke the API using the following command. + +``` +curl --location 'https://default.gw.wso2.com:9095/employees/1.0/employee' \ +--header 'Host: default.gw.wso2.com' \ +--header 'Authorization: Bearer +``` + +Since this guide uses the [httpbin service](https://httpbin.org/anything) which echoes the request and all of its headers, when you invoke the API, you will see the header "Test-Request-Header" with the value "Test-Value". + +### Sample Configurations + +#### Operation Level Request Header Modification + +##### 1. Add Request Header + +``` +- target: "/employee" + verb: "GET" + secured: false + scopes: [] + operationPolicies: + request: + - policyName: AddHeader + policyVersion: v1 + parameters: + headerName: "Header-Name" + headerValue: "Header-Value" +``` + +##### 2. Update Request Header + +``` +- target: "/employee" + verb: "GET" + secured: false + scopes: [] + operationPolicies: + request: + - policyName: SetHeader + policyVersion: v1 + parameters: + headerName: "Header-Name" + headerValue: "Header-Value" +``` + +##### 3. Remove Request Header + +``` +- target: "/employee" + verb: "GET" + secured: false + scopes: [] + operationPolicies: + request: + - policyName: SetHeader + policyVersion: v1 + parameters: + headerName: "Header-Name" +``` + +#### Operation Level Response Header Modification + +##### 1. Add Response Header + +``` +- target: "/employee" + verb: "GET" + secured: false + scopes: [] + operationPolicies: + response: + - policyName: AddHeader + policyVersion: v1 + parameters: + headerName: "Header-Name" + headerValue: "Header-Value" +``` + +##### 2. Update Response Header + +``` +- target: "/employee" + verb: "GET" + secured: false + scopes: [] + operationPolicies: + response: + - policyName: SetHeader + policyVersion: v1 + parameters: + headerName: "Header-Name" + headerValue: "Header-Value" +``` + +##### 3. Remove Response Header + +``` +- target: "/employee" + verb: "GET" + secured: false + scopes: [] + operationPolicies: + response: + - policyName: SetHeader + policyVersion: v1 + parameters: + headerName: "Header-Name" +``` +#### API Level Request Header Modification + +##### 1. Add Request Header + +``` +- target: "/employee" + verb: "GET" + secured: false + scopes: [] +apiPolicies: + request: + - policyName: AddHeader + policyVersion: v1 + parameters: + headerName: "Header-Name" + headerValue: "Header-Value" +``` + +##### 2. Update Request Header + +``` +- target: "/employee" + verb: "GET" + secured: false + scopes: [] +apiPolicies: + request: + - policyName: SetHeader + policyVersion: v1 + parameters: + headerName: "Header-Name" + headerValue: "Header-Value" +``` + +##### 3. Remove Request Header + +``` +- target: "/employee" + verb: "GET" + secured: false + scopes: [] +apiPolicies: + request: + - policyName: SetHeader + policyVersion: v1 + parameters: + headerName: "Header-Name" + +``` + +#### API Level Response Header Modification + +##### 1. Add Response Header + +``` +- target: "/employee" + verb: "GET" + secured: false + scopes: [] +apiPolicies: + response: + - policyName: AddHeader + policyVersion: v1 + parameters: + headerName: "Header-Name" + headerValue: "Header-Value" +``` + +##### 2. Update Response Header + +``` +- target: "/employee" + verb: "GET" + secured: false + scopes: [] +apiPolicies: + response: + - policyName: SetHeader + policyVersion: v1 + parameters: + headerName: "Header-Name" + headerValue: "Header-Value" +``` + +##### 3. Remove Response Header + +``` +- target: "/employee" + verb: "GET" + secured: false + scopes: [] +apiPolicies: + response: + - policyName: SetHeader + policyVersion: v1 + parameters: + headerName: "Header-Name" +``` \ No newline at end of file diff --git a/en/docs/create-api/create-and-attach-api-policies/header-modifier-filters/overview.md b/en/docs/create-api/create-and-attach-api-policies/header-modifier-filters/overview.md new file mode 100644 index 000000000..d5595302c --- /dev/null +++ b/en/docs/create-api/create-and-attach-api-policies/header-modifier-filters/overview.md @@ -0,0 +1,14 @@ +# Header Modification + +The header modification functionality allows the addition, modification, and removal of headers in both requests and responses. This capability is useful for enhancing control and flexibility in API interactions, ensuring that specific requirements for incoming requests and outgoing responses are consistently met. This functionality is configured using the HTTPRoute filters `RequestHeaderModifier` and `ResponseHeaderModifier` filters, which specify how headers should be manipulated at various stages of the request and response lifecycle. + +Header modification can also be achieved using interceptors, as detailed in [this section](../interceptors/interceptors-overview.md). In a scenario where both interceptors and header modification policies are used, **the header modification filters will be applied after the interceptor.** For example. consider the following scenario. + +Assume the GET /employee route is configured with the following. + +1. An interceptor policy to add a header with the name "Interceptor-Header" and the value "Interceptor-Value". +2. A header modification policy to add a header with the name "Modifier-Header" and value "Modifier-Value". + +In this scenario, the interceptor headers will be added **first**, followed by the headers from the header modification policy. + +You can also remove headers added by an interceptor using a header modification policy. Additionally, if you have configured a [mirror policy](../mirror-request-filters/overview.md) for this route, the modified headers will be sent in the mirrored request as well. \ No newline at end of file diff --git a/en/docs/create-api/create-and-attach-api-policies/mirror-request-filters/mirror-request-via-crs.md b/en/docs/create-api/create-and-attach-api-policies/mirror-request-filters/mirror-request-via-crs.md new file mode 100644 index 000000000..43d7ca418 --- /dev/null +++ b/en/docs/create-api/create-and-attach-api-policies/mirror-request-filters/mirror-request-via-crs.md @@ -0,0 +1,97 @@ +# Mirroring Requests via CRs + +This functionality enables request mirroring, where a request can be duplicated and sent to multiple backends for testing and monitoring. This guide explains how to enable request mirroring via Kubernetes CRs. + +### Step 1 - Get the CRs for the relevant API configuration + +Here, you can follow the steps in [Develop and Deploy a REST API via CRs](../../create-and-deploy-apis/rest/create-rest-api-using-crs.md) documentation and create the CRs to deploy an API from scratch. + +Alternatively, you can generate the CRs for a given apk-conf file using the steps as detailed in [this section]({{base_path}}/en/latest/api-management-overview/tools-for-api-development/#option-2-generate-k8s-custom-resources-using-config-generator-tool-and-deploy-the-api-using-kubernetes-client) + +### Step 2 - Add the header modification filters to the HTTPRoute CR + +Header modification can be done using an HTTPRoute filter as follows. + +First, create the relevant Backend CR to mirror the request to. A sample Backend CR is provided below. + +``` +apiVersion: "dp.wso2.com/v1alpha1" +kind: "Backend" +metadata: + name: "mirror-backend" +spec: + services: + - host: "webhook.site" + port: 443 + basePath: "/30cd1472-d4ac-4440-ab8d-66fc0a789c7b" + protocol: "https" +``` + +Then you can refer to this CR in your HTTPRoute CR as follows. + +``` +filters: + - type: "RequestMirror" + requestMirror: + backendRef: + group: "dp.wso2.com" + kind: "Backend" + name: "mirror-backend" +``` + +The complete sample HTTPRoute is as follows. + +``` +apiVersion: "gateway.networking.k8s.io/v1beta1" +kind: "HTTPRoute" +metadata: + name: "production-httproute" +spec: + hostnames: + - "default.gw.wso2.com" + rules: + - matches: + - path: + type: "RegularExpression" + value: "/employee" + method: "GET" + filters: + - type: "RequestMirror" + requestMirror: + backendRef: + group: "dp.wso2.com" + kind: "Backend" + name: "mirror-backend" + parentRefs: + - group: "gateway.networking.k8s.io" + kind: "Gateway" + name: "wso2-apk-default" + sectionName: "httpslistener" +``` + +This policy mirrors the request to the production/sandbox endpoint, as well as the relevant mirror URL. + +### Step 3 - Deploy the API in APK + +You can deploy the API using the following command. Replace with the correct namespace. +``` +kubectl apply -f . -n +``` + +### Step 4 - Generate an access token + +Follow the [Generate Access Token](../../../develop-and-deploy-api/security/generate-access-token.md) documentation to generate an access token. + +### Step 5 - Invoke the API + +You can invoke the API using the following command. + +``` +curl --location 'https://default.gw.wso2.com:9095/employee/1.0/employee' \ +--header 'Host: default.gw.wso2.com' \ +--header 'Authorization: Bearer +``` + +Once you have invoked the above, you can view the duplicated requests in the webhook.site page as follows. + +[![Request Mirroring](../../../assets/img/api-management/api-policies/webhook-site-request-mirroring.png)](../../../assets/img/api-management/api-policies/webhook-site-request-mirroring.png) \ No newline at end of file diff --git a/en/docs/create-api/create-and-attach-api-policies/mirror-request-filters/mirror-request-via-rest-api.md b/en/docs/create-api/create-and-attach-api-policies/mirror-request-filters/mirror-request-via-rest-api.md new file mode 100644 index 000000000..ecb61697c --- /dev/null +++ b/en/docs/create-api/create-and-attach-api-policies/mirror-request-filters/mirror-request-via-rest-api.md @@ -0,0 +1,90 @@ +# Mirroring Requests via APK Conf + +This functionality enables request mirroring, where a request can be duplicated and sent to multiple backends for testing and monitoring. This guide explains how to enable request mirroring via the APK-Conf file. + +### Step 1 - Get the API configuration + +Here, you can follow the steps in [Create an API](../../../get-started/quick-start-guide.md) documentation and save this content into a file named `EmployeeService.apk-conf`. You can use this apk-conf file for the rest of this guide. + +### Step 2 - Add the request mirroring policy to the apk-conf file + +A sample request mirror configuration is given below. +For this guide, it's best to use a webhook.site URL for both the production and request mirroring endpoints to view the duplicate requests. Replace the `WEBHOOK_URL` with a relevant webhook url from the site `https://webhook.site`. Ensure that you keep this webpage open to view the incoming requests. + +``` + - target: "/employee" + verb: "GET" + secured: false + scopes: [] + operationPolicies: + request: + - policyName: RequestMirror + policyVersion: v1 + parameters: + urls: + - `WEBHOOK_URL` +``` + +This policy mirrors the request to the production/sandbox endpoint, as well as the relevant mirror URL. + +The complete apk-conf file with this configuration is given below. + +``` +id: "header-modifier-api" +name: "EmployeeServiceAPI" +basePath: "/employee" +version: "1.0" +type: "REST" +defaultVersion: false +endpointConfigurations: + production: + endpoint: `WEBHOOK_URL` +operations: +- target: "/employee" + verb: "GET" + secured: false + scopes: [] + operationPolicies: + request: + - policyName: RequestMirror + policyVersion: v1 + parameters: + urls: + - `WEBHOOK_URL` +- target: "/employee" + verb: "POST" + secured: true + scopes: [] +- target: "/employee/{employeeId}" + verb: "PUT" + secured: true + scopes: [] +- target: "/employee/{employeeId}" + verb: "DELETE" + secured: true + scopes: [] +``` + +### Step 3 - Deploy the API in APK + +Refer to the [Deploy the API in APK](../../../get-started/quick-start-guide.md#deploy-the-api-in-apk) to deploy the API using APK configuration. + +### Step 4 - Generate an access token + +Follow the [Generate Access Token](../../../develop-and-deploy-api/security/generate-access-token.md) documentation to generate an access token. + +### Step 5 - Invoke the API + +You can invoke the API using the following command. + +``` +curl --location 'https://default.gw.wso2.com:9095/employee/1.0/employee' \ +--header 'Host: default.gw.wso2.com' \ +--header 'Authorization: Bearer +``` + +Once you have invoked the above, you can view the duplicated requests in the webhook.site page as follows. + +[![Request Mirroring](../../../assets/img/api-management/api-policies/webhook-site-request-mirroring.png)](../../../assets/img/api-management/api-policies/webhook-site-request-mirroring.png) + + diff --git a/en/docs/create-api/create-and-attach-api-policies/mirror-request-filters/overview.md b/en/docs/create-api/create-and-attach-api-policies/mirror-request-filters/overview.md new file mode 100644 index 000000000..838e3de12 --- /dev/null +++ b/en/docs/create-api/create-and-attach-api-policies/mirror-request-filters/overview.md @@ -0,0 +1,10 @@ +# Request Mirroring + +Request mirroring allows traffic to be duplicated and sent to additional backends for testing and monitoring purposes. This feature is particularly useful for scenarios where you want to test changes or monitor the behavior of your application under different conditions without affecting the primary production or sandbox environment. The Request Mirror filter can mirror requests to different backends apart from the primary backend defined in the production or sandbox endpoints. + +The current implementation operates in a “fire and forget” manner, and only the response from the primary endpoint will be returned. This allows for the assessment of changes and performance in a real-world environment without impacting the actual user experience. This method ensures that the production environment remains stable while providing insights into how updates or modifications would perform under actual load conditions. + +Request mirroring can be done in one of two ways. + +1. Via REST API (using the APK-Conf file). +2. Via CRs. diff --git a/en/docs/create-api/create-and-attach-api-policies/redirect-request-filters/redirect-request-via-crs.md b/en/docs/create-api/create-and-attach-api-policies/redirect-request-filters/redirect-request-via-crs.md new file mode 100644 index 000000000..53bb93248 --- /dev/null +++ b/en/docs/create-api/create-and-attach-api-policies/redirect-request-filters/redirect-request-via-crs.md @@ -0,0 +1,92 @@ +# Request Redirection via REST API + +Request redirection allows you to send clients to a different URL. This can be useful for changing old URLs to new ones or directing traffic based on specific conditions. Request redirection can be configured via Kubernetes CRs as follows. + +### Step 1 - Get the CRs for the relevant API configuration + +Here, you can follow the steps in [Develop and Deploy a REST API via CRs](../../create-and-deploy-apis/rest/create-rest-api-using-crs.md) documentation and create the CRs to deploy an API from scratch. + +Alternatively, you can generate the CRs for a given apk-conf file using the steps as detailed in [this section]({{base_path}}/en/latest/api-management-overview/tools-for-api-development/#option-2-generate-k8s-custom-resources-using-config-generator-tool-and-deploy-the-api-using-kubernetes-client) + +### Step 2 - Add the request redirection filters to the HTTPRoute CR + +A sample request redirection configuration for an HTTPRoute is given below. + +``` +filters: +- type: "RequestRedirect" + requestRedirect: + scheme: "https" + hostname: "httpbin.org" + path: + type: "ReplaceFullPath" + replaceFullPath: "/anything" + statusCode: 301 +``` + +This policy will redirect an incoming request to the GET /employee route with a 301 status code. + +The complete HTTPRoute with this configuration is given below. + +``` +apiVersion: "gateway.networking.k8s.io/v1beta1" +kind: "HTTPRoute" +metadata: + name: "production-httproute" +spec: + hostnames: + - "default.gw.wso2.com" + rules: + - matches: + - path: + type: "RegularExpression" + value: "/employee" + method: "GET" + filters: + - type: "RequestRedirect" + requestRedirect: + scheme: "https" + hostname: "httpbin.org" + path: + type: "ReplaceFullPath" + replaceFullPath: "/anything" + statusCode: 301 + parentRefs: + - group: "gateway.networking.k8s.io" + kind: "Gateway" + name: "wso2-apk-default" + sectionName: "httpslistener" +``` + +!!! Note + An HTTPRoute rule that has a RequestRedirect filter cannot have a backendRef or a URLRewrite filter attached. + +### Step 3 - Deploy the API in APK + +You can deploy the API using the following command. Replace with the correct namespace. +``` +kubectl apply -f . -n +``` + +### Step 4 - Generate an access token + +Follow the [Generate Access Token](../../../develop-and-deploy-api/security/generate-access-token.md) documentation to generate an access token. + +### Step 5 - Invoke the API + +You can invoke the API using the following command. + +``` +curl --location 'https://default.gw.wso2.com:9095/employee/1.0/employee' \ +--header 'Host: default.gw.wso2.com' \ +--header 'Authorization: Bearer -I +``` + +Once you have invoked the above, you will see the 301 status code and the `Location` header containing the new URL. + +``` +HTTP/2 301 +location: https://httpbin.org/anything?id=1 +vary: Accept-Encoding +server: envoy +``` \ No newline at end of file diff --git a/en/docs/create-api/create-and-attach-api-policies/redirect-request-filters/redirect-request-via-rest-api.md b/en/docs/create-api/create-and-attach-api-policies/redirect-request-filters/redirect-request-via-rest-api.md new file mode 100644 index 000000000..2e089ba95 --- /dev/null +++ b/en/docs/create-api/create-and-attach-api-policies/redirect-request-filters/redirect-request-via-rest-api.md @@ -0,0 +1,92 @@ +# Request Redirection via REST API + +Request redirection allows you to send clients to a different URL. This can be useful for changing old URLs to new ones or directing traffic based on specific conditions. Request redirection can be configured via the apk-conf file as follows. + +### Step 1 - Get the API configuration + +Here, you can follow the steps in [Create an API](../../../get-started/quick-start-guide.md) documentation and save this content into a file named `EmployeeService.apk-conf`. You can use this apk-conf file for the rest of this guide. + +### Step 2 - Add the request redirection policy to the apk-conf file + +A sample request redirection configuration is given below. + +``` +- target: "/employee" + verb: "GET" + secured: false + scopes: [] + operationPolicies: + request: + - policyName: RequestRedirect + policyVersion: v1 + parameters: + url: https://httpbin.org/anything + statusCode: 301 +``` + +This policy will redirect an incoming request to the GET /employee route with a 301 status code. + +The complete apk-conf file with this configuration is given below. + +``` +id: "redirect-request-api" +name: "EmployeeServiceAPI" +basePath: "/employee" +version: "1.0" +type: "REST" +defaultVersion: false +endpointConfigurations: + production: + endpoint: https://httpbin.org/anything +operations: +- target: "/employee" + verb: "GET" + secured: false + scopes: [] + operationPolicies: + request: + - policyName: RequestRedirect + policyVersion: v1 + parameters: + url: https://httpbin.org/anything + statusCode: 301 +- target: "/employee" + verb: "POST" + secured: true + scopes: [] +- target: "/employee/{employeeId}" + verb: "PUT" + secured: true + scopes: [] +- target: "/employee/{employeeId}" + verb: "DELETE" + secured: true + scopes: [] +``` +### Step 3 - Deploy the API in APK + +Refer to the [Deploy the API in APK](../../../get-started/quick-start-guide.md#deploy-the-api-in-apk) to deploy the API using APK configuration. + +### Step 4 - Generate an access token + +Follow the [Generate Access Token](../../../develop-and-deploy-api/security/generate-access-token.md) documentation to generate an access token. + +### Step 5 - Invoke the API + +You can invoke the API using the following command. + +``` +curl --location 'https://default.gw.wso2.com:9095/employee/1.0/employee' \ +--header 'Host: default.gw.wso2.com' \ +--header 'Authorization: Bearer -I +``` + +Once you have invoked the above, you will see the 301 status code and the `Location` header containing the new URL. + +``` +HTTP/2 301 +location: https://httpbin.org/anything?id=1 +vary: Accept-Encoding +date: Mon, 08 Jul 2024 08:05:57 GMT +server: envoy +``` \ No newline at end of file diff --git a/en/mkdocs.yml b/en/mkdocs.yml index c324eb744..aaf6be5af 100644 --- a/en/mkdocs.yml +++ b/en/mkdocs.yml @@ -123,6 +123,17 @@ nav: - CORS: - Via REST API: create-api/create-and-attach-api-policies/cors/enable-cors-via-rest-api.md - Via CRs: create-api/create-and-attach-api-policies/cors/enable-cors-via-crs.md + - Header Modification: + - Overview: create-api/create-and-attach-api-policies/header-modifier-filters/overview.md + - Via REST API: create-api/create-and-attach-api-policies/header-modifier-filters/header-modifier-via-rest-api.md + - Via CRs: create-api/create-and-attach-api-policies/header-modifier-filters/header-modifier-via-crs.md + - Request Mirroring: + - Overview: create-api/create-and-attach-api-policies/mirror-request-filters/overview.md + - Via REST API: create-api/create-and-attach-api-policies/mirror-request-filters/mirror-request-via-rest-api.md + - Via CRs: create-api/create-and-attach-api-policies/mirror-request-filters/mirror-request-via-crs.md + - Request Redirection: + - Via REST API: create-api/create-and-attach-api-policies/redirect-request-filters/redirect-request-via-rest-api.md + - Via CRs: create-api/create-and-attach-api-policies/redirect-request-filters/redirect-request-via-crs.md - Rate Limiting: - Overview: create-api/rate-limiting/rate-limiting-policy-overview.md - Simple Rate Limiting: