Home > Articles

Go HTTP Servers

  • Print
  • + Share This
This chapter is from the book

Examining Requests and Responses

As this hour progresses, we will examine requests and responses that are being sent and received. The tool we use for this is curl.

Curl is a command-line tool for making HTTP requests, and is generally available on all platforms. On macOS, curl is pre-installed. On Linux, curl is often installed and is available through package managers. For Windows, curl is not pre-installed. For Windows users, installing GIT for Windows was recommended in an earlier chapter. If you have not done this, visit https://git-scm.com/download/win, open the download file, and install it. Once installed, you will have a new Start menu item: Git Bash. Open it.

To verify everything is installed correctly, follow these steps:

  • 1a. On macOS or Linux, open a terminal.

  • 1b. On Windows, open “Git Bash” from the Start menu.

  • 2. Type curl and hit Return.

  • 3. If curl is installed successfully, you will see the output shown in Figure 18.1.


FIGURE 18.1 Verifying curl is correctly installed.

Making a Request with curl

With curl installed, you can use it for developing and debugging web servers. Rather than using a browser, you can use curl to send a variety of requests to a web server and to examine the response. To make a request to the Hello World web server, open a terminal and run the server.

go run example01.go

On macOS or Linux, open another terminal tab or window. On Windows, switch to Git Bash. Run the following command. The -is options mean that headers are printed and that some unwanted output is ignored.

curl -is http://localhost:8000

If the command was successful, you will see a response from the web server that includes the headers and the response body.

HTTP/1.1 200 OK
Date: Wed, 16 Nov 2016 16:45:51 GMT
Content-Length: 12
Content-Type: text/plain; charset=utf-8

Hello World

The output may be explained as follows:

  • The response uses the HTTP 1.1 protocol, and a 200 response was received.

  • The Date header details when the response was sent.

  • The Content-Length header details the length of the response. In this case, it is 12 bytes.

  • The Content-Type header details the type of content and the encoding used. In this case, the response is text/plain encoded using utf-8.

  • Finally, the response body is outputted. In this case, it is Hello World.

Routing in More Detail

The HandleFunc registers functions to respond to URL address mappings. In simplistic terms, HandleFunc sets up a routing table that allows the HTTP server to respond correctly.

http.HandleFunc("/", helloWorld)
http.HandleFunc("/users/", usersHandler)
http.HandleFunc("/projects/", projectsHandler)

In this example, whenever a request is made to /, the helloWorld function will be called. Whenever a request is made to /users/, the usersHandler function will be called, and so on.

Note the following points about the behavior of the router:

  • The default router directs any request that it does not have a handler for to /.

  • The route must match exactly; for example, a request to /users will go to /, as it is missing a trailing slash.

  • The router has no concern over the type of request. It simply passes a request that matches a route to the handler.

  • + Share This
  • 🔖 Save To Your Account