HTTP Method Matching

You can also create route rules based on the request HTTP method, e.g. GET, POST, DELETE, etc. You can specify one or more HTTP Methods to match against, and if any one of those method verbs is present, the request will match, that is Gloo will conditional OR the match for HTTP Method. Note: since Gloo/Envoy is based on HTTP/2, this gets translated into a header value match against the HTTP/2 :method header, which by spec includes all of the HTTP/1 verbs.

Setup

This guide assumes that you have deployed Gloo to the gloo-system namespace and that the glooctl command line utility is installed on your machine. glooctl provides several convenient functions to view, manipulate, and debug Gloo resources; in particular, it is worth mentioning the following command, which we will use each time we need to retrieve the URL of the Gloo Gateway that is running inside your cluster:

glooctl proxy url

Let’s create a simple upstream for testing called json-upstream, that routes to a static site:


apiVersion: gloo.solo.io/v1
kind: Upstream
metadata:
  name: json-upstream
  namespace: gloo-system
spec:
  static:
    hosts:
      - addr: jsonplaceholder.typicode.com
        port: 80

glooctl create upstream static --static-hosts jsonplaceholder.typicode.com:80 --name json-upstream

Example

Let’s create a virtual service with an http method match on POST:


apiVersion: gateway.solo.io/v1
kind: VirtualService
metadata:
  name: test-post
  namespace: gloo-system
spec:
  virtualHost:
    domains:
      - 'foo'
    routes:
      - matchers:
         - methods:
            - POST
           prefix: /
        routeAction:
          single:
            upstream:
              name: json-upstream
              namespace: gloo-system
        options:
          autoHostRewrite: true

Let’s POST to that route and make sure it works:

curl -H "Host: foo" -XPOST $(glooctl proxy url)/posts

returns

{
  "id": 101
}

Let’s delete that virtual service.


kubectl delete vs -n gloo-system test-post

glooctl delete vs --name test-post

Now let’s create a virtual service that matches on GET:


apiVersion: gateway.solo.io/v1
kind: VirtualService
metadata:
  name: test-get
  namespace: gloo-system
spec:
  virtualHost:
    domains:
      - 'foo'
    routes:
      - matchers:
         - methods:
            - GET
           prefix: /
        routeAction:
          single:
            upstream:
              name: json-upstream
              namespace: gloo-system
        options:
          autoHostRewrite: true

Now POST requests will return a 404:

curl -v -H "Host: foo" -XPOST $(glooctl proxy url)/posts

But GET requests succeed:

curl -H "Host: foo" $(glooctl proxy url)/posts

Summary

In this guide, we created a virtual service that utilized HTTP method matching and demonstrated it on POST and GET requests.

Cleanup the resources by running:


kubectl delete vs -n gloo-system test-get
kubectl delete upstream -n gloo-system json-upstream

glooctl delete vs test-get
glooctl delete upstream json-upstream