We're sorry but this app doesn't work properly without JavaScript enabled. Please enable it to continue.

This lesson's interactive features are locked, please to keep using them

Routing

The Go standard library has a lot of powerful HTTP features and, as of version 1.22, comes equipped with method-based pattern matching for routing.

In this lesson, we are going to limit which endpoints are available via which HTTP methods. In our current implementation, we can use any HTTP method to access any endpoint. This is not ideal.

There are powerful routing libraries like Gorilla Mux and Chi, however, the course will assume you are using Go's standard library. Just know that it isn't your only option!

Try It

Run this command to send an empty POST request to your running server:

curl -X POST http://localhost:8080/healthz

You should get an OK response - but we want this endpoint to only be available via GET requests!

Method Specific Routing

Using the Go standard library, you can specify a method like this: [METHOD ][HOST]/[PATH]. For example:

mux.HandleFunc("POST /articles", handlerArticlesCreate)
mux.HandleFunc("DELETE /articles", handlerArticlesDelete)

Assignment

    When a request is made to one of these endpoints with a method other than GET, the server should return a 405 (Method Not Allowed) response (this is handled automatically!).

Run and submit the CLI tests.