|
| 1 | +# HTTP query parameter matching |
| 2 | + |
| 3 | +???+ info "Extended Support Feature: HTTPRouteQueryParamMatching" |
| 4 | + This feature is part of extended support. For more information on release channels, refer to our [versioning guide](../concepts/versioning.md). |
| 5 | + |
| 6 | +The [HTTPRoute resource](../api-types/httproute.md) can be used to match |
| 7 | +requests based on query parameters. This guide shows how to use this |
| 8 | +functionality. |
| 9 | + |
| 10 | +## Matching requests based on a single query parameter |
| 11 | + |
| 12 | +The following `HTTPRoute` splits traffic between two backends based on the |
| 13 | +value of the `animal` query parameter: |
| 14 | + |
| 15 | +```yaml |
| 16 | +apiVersion: gateway.networking.k.io/v1 |
| 17 | +kind: HTTPRoute |
| 18 | +metadata: |
| 19 | + name: query-param-matching |
| 20 | + namespace: gateway-conformance-infra |
| 21 | +spec: |
| 22 | + parentRefs: |
| 23 | + - name: same-namespace |
| 24 | + rules: |
| 25 | + - matches: |
| 26 | + - queryParams: |
| 27 | + - name: animal |
| 28 | + value: whale |
| 29 | + backendRefs: |
| 30 | + - name: infra-backend-v1 |
| 31 | + port: 8080 |
| 32 | + - matches: |
| 33 | + - queryParams: |
| 34 | + - name: animal |
| 35 | + value: dolphin |
| 36 | + backendRefs: |
| 37 | + - name: infra-backend-v2 |
| 38 | + port: 8080 |
| 39 | +``` |
| 40 | +
|
| 41 | +- A request to `/` with the query parameter `animal=whale` will be routed to `infra-backend-v1`. |
| 42 | +- A request to `/` with the query parameter `animal=dolphin` will be routed to `infra-backend-v2`. |
| 43 | + |
| 44 | +## Matching requests based on multiple query parameters |
| 45 | + |
| 46 | +A rule can also match on multiple query parameters. The following rule routes |
| 47 | +traffic to `infra-backend-v3` if the query parameters `animal=dolphin` AND |
| 48 | +`color=blue` are present: |
| 49 | + |
| 50 | +```yaml |
| 51 | + - matches: |
| 52 | + - queryParams: |
| 53 | + - name: animal |
| 54 | + value: dolphin |
| 55 | + - name: color |
| 56 | + value: blue |
| 57 | + backendRefs: |
| 58 | + - name: infra-backend-v3 |
| 59 | + port: 8080 |
| 60 | +``` |
| 61 | + |
| 62 | +## ORing matches |
| 63 | + |
| 64 | +If a rule has multiple `matches`, a request will be routed if it satisfies any |
| 65 | +of them. The following rule routes traffic to `infra-backend-v3` if: |
| 66 | + |
| 67 | +- The query parameters `animal=dolphin` AND `color=blue` are present. |
| 68 | +- OR the query parameter `ANIMAL=Whale` is present. |
| 69 | + |
| 70 | +```yaml |
| 71 | + - matches: |
| 72 | + - queryParams: |
| 73 | + - name: animal |
| 74 | + value: dolphin |
| 75 | + - name: color |
| 76 | + value: blue |
| 77 | + - queryParams: |
| 78 | + - name: ANIMAL |
| 79 | + value: Whale |
| 80 | + backendRefs: |
| 81 | + - name: infra-backend-v3 |
| 82 | + port: 8080 |
| 83 | +``` |
| 84 | + |
| 85 | +## Combining with other match types |
| 86 | + |
| 87 | +Query parameter matching can be combined with other match types like path and |
| 88 | +header matching. The following rules demonstrate this: |
| 89 | + |
| 90 | +```yaml |
| 91 | + - matches: |
| 92 | + - path: |
| 93 | + type: PathPrefix |
| 94 | + value: /path1 |
| 95 | + queryParams: |
| 96 | + - name: animal |
| 97 | + value: whale |
| 98 | + backendRefs: |
| 99 | + - name: infra-backend-v1 |
| 100 | + port: 8080 |
| 101 | + - matches: |
| 102 | + - headers: |
| 103 | + - name: version |
| 104 | + value: one |
| 105 | + queryParams: |
| 106 | + - name: animal |
| 107 | + value: whale |
| 108 | + backendRefs: |
| 109 | + - name: infra-backend-v2 |
| 110 | + port: 8080 |
| 111 | + - matches: |
| 112 | + - path: |
| 113 | + type: PathPrefix |
| 114 | + value: /path2 |
| 115 | + headers: |
| 116 | + - name: version |
| 117 | + value: two |
| 118 | + queryParams: |
| 119 | + - name: animal |
| 120 | + value: whale |
| 121 | + backendRefs: |
| 122 | + - name: infra-backend-v3 |
| 123 | + port: 8080 |
| 124 | +``` |
0 commit comments