Basic HTTP Server in Go

GolangGolangBeginner
Practice Now

This tutorial is from open-source community. Access the source code

Introduction

This lab aims to test your ability to write a basic HTTP server using the net/http package in Golang.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL go(("`Golang`")) -.-> go/NetworkingGroup(["`Networking`"]) go/NetworkingGroup -.-> go/http_server("`HTTP Server`") subgraph Lab Skills go/http_server -.-> lab-15482{{"`Basic HTTP Server in Go`"}} end

HTTP Server

You are required to write a simple HTTP server that can handle two routes: /hello and /headers. The /hello route should return a simple "hello" response, while the /headers route should return all the HTTP request headers.

  • The server should use the net/http package.
  • The /hello route should return a "hello" response.
  • The /headers route should return all the HTTP request headers.
  • The server should listen on port 8090.
## Run the server in the background.
$ go run http-servers.go &

## Access the `/hello` route.
$ curl localhost:8090/hello
hello

There is the full code below:

// Writing a basic HTTP server is easy using the
// `net/http` package.
package main

import (
	"fmt"
	"net/http"
)

// A fundamental concept in `net/http` servers is
// *handlers*. A handler is an object implementing the
// `http.Handler` interface. A common way to write
// a handler is by using the `http.HandlerFunc` adapter
// on functions with the appropriate signature.
func hello(w http.ResponseWriter, req *http.Request) {

	// Functions serving as handlers take a
	// `http.ResponseWriter` and a `http.Request` as
	// arguments. The response writer is used to fill in the
	// HTTP response. Here our simple response is just
	// "hello\n".
	fmt.Fprintf(w, "hello\n")
}

func headers(w http.ResponseWriter, req *http.Request) {

	// This handler does something a little more
	// sophisticated by reading all the HTTP request
	// headers and echoing them into the response body.
	for name, headers := range req.Header {
		for _, h := range headers {
			fmt.Fprintf(w, "%v: %v\n", name, h)
		}
	}
}

func main() {

	// We register our handlers on server routes using the
	// `http.HandleFunc` convenience function. It sets up
	// the *default router* in the `net/http` package and
	// takes a function as an argument.
	http.HandleFunc("/hello", hello)
	http.HandleFunc("/headers", headers)

	// Finally, we call the `ListenAndServe` with the port
	// and a handler. `nil` tells it to use the default
	// router we've just set up.
	http.ListenAndServe(":8090", nil)
}

Summary

In this lab, you were required to write a simple HTTP server that can handle two routes: /hello and /headers. You learned how to use the net/http package to write handlers for each route and register them on the server. Finally, you learned how to start the server and listen for incoming requests.

Other Golang Tutorials you may like