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 as http or https.
  • :authority: The hostname and port number that the request is sent to.
  • :path: The path of the request.

Before you begin

  1. Set up an agentgateway proxy.
  2. Install the httpbin sample app.

Update request paths and HTTP methods

  1. Create an AgentgatewayPolicy resource with your transformation rules. The policy rewrites the path to /post and the method to POST when the foo: bar request 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'
    EOF
  2. Send a request to the /get endpoint and include the foo: bar header to trigger the transformation. Verify that you get back a 200 HTTP response code. The httpbin /post endpoint only accepts POST requests, 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
    }
    
  3. Send another request to the /get endpoint. This time, omit the foo: bar header. 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
Agentgateway assistant

Ask me anything about agentgateway configuration, features, or usage.

Note: AI-generated content might contain errors; please verify and test all returned information.

Tip: one topic per conversation gives the best results. Use the + button in the chat header to start a new conversation.

Switching topics? Starting a new conversation improves accuracy.
↑↓ navigate select esc dismiss

What could be improved?

Your feedback helps us improve assistant answers and identify docs gaps we should fix.

Need more help? Join us on Discord: https://discord.gg/y9efgEmppm

Want to use your own agent? Add the Solo MCP server to query our docs directly. Get started here: https://search.solo.io/.