You can handle different types of HTTP requests like GET, POST, DELETE, PUT, PATCH, etc using the net/http
package in Go and execute different logic based on the request type.
It is easily possible by checking the HTTP method of the current request in the Golang server.
You can access the request method from the *http.Request
parameter in your route handler.
How to Check HTTP Request Method in Go
When you register a route in Golang using the net/http
package, you get access to the *http.Request
type which has a Method
property.
This property contains the method name of the request as a string. By default, every route only handles the GET
requests.
package main
import (
"fmt"
"net/http"
)
func messageHandler(w http.ResponseWriter, r *http.Request) {
fmt.Println(r.Method) // GET
fmt.Fprint(w, "Handling GET request")
}
func main() {
http.HandleFunc("/message", messageHandler)
// Start the server on port 8000
fmt.Println("Server started: http://localhost:8000")
err := http.ListenAndServe(":8000", nil)
if err != nil {
fmt.Println("Error starting server:", err)
}
}
Here I have a "/message" route. If I print the value of r.Method
inside the route handler function and send a GET request, its value will be "GET".
- For Get requests, it will return "GET"
- For Post requests, it will return "POST"
- For Delete requests, it will return "DELETE"
- For Put requests, it will return "PUT"
- For Patch requests, it will return "PATCH"
Now if you want to handle different types of HTTP methods in your route, you can check for these values.
For example, if you want to do something different for the POST request, you can check if the r.Method
is equal to "POST" or not.
func messageHandler(w http.ResponseWriter, r *http.Request) {
if r.Method == "POST" {
fmt.Fprint(w, "Handling POST request")
}
}
If the r.Method
property contains the value "POST" then you know it's a Post request and you can execute your logic accordingly.
Every time you want to check the method type, you have to type those method names (GET, POST, DELETE, etc). There is a chance you might misspell the name.
So the http
package provides some constants that hold those method names. You can use those constants to check the method types without typing the names manually.
These constants are:
http.MethodGet
is equal to "GET"http.MethodPost
is equal to "POST"http.MethodDelete
is equal to "DELETE"http.MethodPut
is equal to "PUT"http.MethodPatch
is equal to "PATCH"
func messageHandler(w http.ResponseWriter, r *http.Request) {
if r.Method == http.MethodPost {
fmt.Fprint(w, "Handling POST request")
}
}
Handling GET Method
func messageHandler(w http.ResponseWriter, r *http.Request) {
if r.Method == http.MethodGet {
fmt.Fprint(w, "Handling GET request")
}
}
Handling POST Method
func messageHandler(w http.ResponseWriter, r *http.Request) {
if r.Method == http.MethodPost {
fmt.Fprint(w, "Handling POST request")
}
}
Handling DELETE Method
func messageHandler(w http.ResponseWriter, r *http.Request) {
if r.Method == http.MethodDelete {
fmt.Fprint(w, "Handling DELETE request")
}
}
Handling PUT Method
func messageHandler(w http.ResponseWriter, r *http.Request) {
if r.Method == http.MethodPut {
fmt.Fprint(w, "Handling PUT request")
}
}
Handling PATCH Method
func messageHandler(w http.ResponseWriter, r *http.Request) {
if r.Method == http.MethodPatch {
fmt.Fprint(w, "Handling PATCH request")
}
}
Handling Multiple Request Methods
It is also possible to handle more than one HTTP request method by one route. You just have to check the method type of the current request and implement your logic according to the method type.
package main
import (
"fmt"
"net/http"
)
func messageHandler(w http.ResponseWriter, r *http.Request) {
if r.Method == http.MethodGet {
// Get method
handleGet(w, r)
} else if r.Method == http.MethodPost {
// Post method
handlePost(w, r)
} else if r.Method == http.MethodDelete {
// Delete method
handleDelete(w, r)
} else if r.Method == http.MethodPut {
// Put method
handlePut(w, r)
} else if r.Method == http.MethodPatch{
// Patch method
handlePatch(w, r)
} else {
// Throw an error if the method doesn't match
http.Error(w, "Methodnot allowed", http.StatusMethodNotAllowed)
}
}
func handleGet(w http.ResponseWriter, r *http.Request) {
// Implement your GET logic here
fmt.Fprint(w, "Handling GET request")
}
func handlePost(w http.ResponseWriter, r *http.Request) {
// Implement your POST logic here
fmt.Fprint(w, "Handling POST request")
}
func handleDelete(w http.ResponseWriter, r *http.Request) {
// Implement your DELETE logic here
fmt.Fprint(w, "Handling DELETE request")
}
func handlePut(w http.ResponseWriter, r *http.Request) {
// Implement your PUT logic here
fmt.Fprint(w, "Handling PUT request")
}
func handlePatch(w http.ResponseWriter, r *http.Request) {
// Implement your PATCH logic here
fmt.Fprint(w, "Handling PATCH request")
}
func main() {
http.HandleFunc("/message", messageHandler)
// Start the server on port 8000
fmt.Println("Server started: http://localhost:8000")
err := http.ListenAndServe(":8000", nil)
if err != nil {
fmt.Println("Error starting server:", err)
}
}
I am using if
statement to check multiple method types. If the r.Method
is equal to GET
, the handleGet()
function will be executed etc.
If it doesn't match any of the methods that you want to handle in your route, you can throw an error.
You can also use the switch
statement instead of the if
statement if you want. Both of them will do the same thing.
func messageHandler(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case http.MethodGet:
// Get method
handleGet(w, r)
case http.MethodPost:
// Post method
handlePost(w, r)
case http.MethodDelete:
// Delete method
handleDelete(w, r)
case http.MethodPut:
// Put method
handlePut(w, r)
case http.MethodPatch:
// Patch method
handlePatch(w, r)
default:
// Throw an error if the method doesn't match
http.Error(w, "Method is not allowed", http.StatusMethodNotAllowed)
}
}
In this way, you can handle multiple HTTP request methods inside a Golang server route.