Change request path and method
Verified Code examples on this page have been automatically tested and verified.Change the request path and HTTP method when a request header is present by using CEL expressions. The example uses request.headers[], request.path, and request.method with a ternary expression to conditionally set the :path and :method pseudo headers.
About pseudo headers
Pseudo headers are special headers that are used in HTTP/2 to provide metadata about the request or response in a structured way. Although they look like traditional HTTP/1.x headers, they come with specific characteristics:
- Must always start with a colon (
:). - Must appear before regular headers in the HTTP/2 frame.
- Contain details about the request or response.
Common pseudo headers include:
:method: The HTTP method that is used, such as GET or POST.:scheme: The protocol that is used, such ashttporhttps.:authority: The hostname and port number that the request is sent to.:path: The path of the request.
Before you begin
- Set up an agentgateway proxy.
- Install the httpbin sample app.
Update request paths and HTTP methods
Create an AgentgatewayPolicy resource with your transformation rules. The policy rewrites the path to
/postand the method toPOSTwhen thefoo: barrequest header is present. When the header is absent, the path and method are unchanged.kubectl apply -f- <<EOF apiVersion: agentgateway.dev/v1alpha1 kind: AgentgatewayPolicy metadata: name: transformation namespace: httpbin spec: targetRefs: - group: gateway.networking.k8s.io kind: HTTPRoute name: httpbin traffic: transformation: request: set: - name: ":path" value: 'request.headers["foo"] == "bar" ? "/post" : request.path' - name: ":method" value: 'request.headers["foo"] == "bar" ? "POST" : request.method' EOFSend a request to the
/getendpoint and include thefoo: barheader to trigger the transformation. Verify that you get back a 200 HTTP response code. The httpbin/postendpoint only acceptsPOSTrequests, so a 200 response confirms both the path rewrite and the method change succeeded.curl -vi http://$INGRESS_GW_ADDRESS:80/get \ -H "foo: bar" \ -H "host: www.example.com:80"curl -vi localhost:8080/get \ -H "foo: bar" \ -H "host: www.example.com"Example output:
< HTTP/1.1 200 OK HTTP/1.1 200 OK ... { "args": {}, "headers": { "Accept": [ "*/*" ], "Content-Length": [ "0" ], "Foo": [ "bar" ], "Host": [ "www.example.com:8080" ], "User-Agent": [ "curl/7.77.0" ] }, "origin": "127.0.0.6:48539", "url": "http://www.example.com:8080/post", "data": "", "files": null, "form": null, "json": null }Send another request to the
/getendpoint. This time, omit thefoo: barheader. Verify that you get back a 200 HTTP response code and that the request path is not rewritten.curl -vi http://$INGRESS_GW_ADDRESS:80/get \ -H "host: www.example.com:80"curl -vi localhost:8080/get \ -H "host: www.example.com"Example output:
< HTTP/1.1 200 OK HTTP/1.1 200 OK ... { "args": {}, "headers": { "Accept": [ "*/*" ], "Host": [ "www.example.com:8080" ], "User-Agent": [ "curl/7.77.0" ] }, "origin": "127.0.0.6:46209", "url": "http://www.example.com:8080/get" }
Cleanup
You can remove the resources that you created in this guide.kubectl delete AgentgatewayPolicy transformation -n httpbin