create a http server with a post route in go

Here is an example of how to create an HTTP server with a post route in Go:

main.go
package main

import (
	"fmt"
	"log"
	"net/http"
)

func main() {
	http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
		if r.Method == "POST" {
			// Handle POST request here
			fmt.Fprintf(w, "Received POST request\n")
		} else {
			// Handle all other requests
			fmt.Fprintf(w, "Hello, World\n")
		}
	})

	log.Fatal(http.ListenAndServe(":8080", nil))
}
373 chars
22 lines

This code sets up an HTTP server that listens on port 8080. It then defines a request handler function that checks if the incoming request is a POST request. If it is, it sends a simple response message. If the request is not a POST request, it sends a "Hello, World" message.

Note that this example does not include any actual handling of the data sent in the POST request body. Depending on your use case, you may need to parse and process the data differently.

related categories

gistlibby LogSnag