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 you’ve installed Gloo into the gloo-system namespace, and glooctl is installed to your machine. Let’s also make sure $GATEWAY_URL is set in your terminal:

export GATEWAY_URL=$(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:
  upstreamSpec:
    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:
      - matcher:
          methods:
            - POST
          prefix: /
        routeAction:
          single:
            upstream:
              name: json-upstream
              namespace: gloo-system

glooctl create vs --name test-post --namespace gloo-system --domains foo
glooctl add route --name test-post --method POST  --path-prefix / --dest-name json-upstream

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

curl -H "Host: foo" -XPOST $GATEWAY_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:
      - matcher:
          methods:
            - GET
          prefix: /
        routeAction:
          single:
            upstream:
              name: json-upstream
              namespace: gloo-system

glooctl create vs --name test-get --namespace gloo-system --domains foo
glooctl add route --name test-get --method GET  --path-prefix / --dest-name json-upstream

Now POST requests will return a 404:

curl -v -H "Host: foo" -XPOST $GATEWAY_URL/posts

But GET requests succeed:

curl -H "Host: foo" $GATEWAY_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