HTTP request handling in Go¶
The Go standard library includes a robust built-in package for HTTP communication, accessible via net/http.^[400-devops-09-scripting-language-golang-introduction-part-3http-readme.md] This package enables the creation of HTTP clients and servers without external dependencies.^[400-devops-09-scripting-language-golang-introduction-part-3http-readme.md]
Setting up the Server¶
To start an HTTP server, the http.HandleFunc function is used to register a handler function for a specific route pattern, and http.ListenAndServe is used to start the server on a specified port.^[400-devops-09-scripting-language-golang-introduction-part-3http-readme.md]
When registering a handler, the provided function must match the signature func(ResponseWriter, *Request).^[400-devops-09-scripting-language-golang-introduction-part-3http-readme.md] The ListenAndServe function blocks the program execution while the server runs; if the handler is set to nil, it defaults to DefaultServeMux.^[400-devops-09-scripting-language-golang-introduction-part-3http-readme.md]
The Request Handler¶
A request handler function accepts two arguments: an http.ResponseWriter and an *http.Request.^[400-devops-09-scripting-language-golang-introduction-part-3http-readme.md]
http.ResponseWriter: Used to construct and send the response back to the client.*http.Request: Contains the incoming request details, including the URL, headers, and body.
Sending a Response¶
To write data to the client, the Write() method on the ResponseWriter is used, which accepts a byte slice ([]byte).^[400-devops-09-scripting-language-golang-introduction-part-3http-readme.md] Sending a string typically requires casting it to bytes, such as []byte("Hello!").^[400-devops-09-scripting-language-golang-introduction-part-3http-readme.md]
Managing Headers¶
HTTP headers are accessible via the Header field of the Request struct, which acts as a map of keys to values.^[400-devops-09-scripting-language-golang-introduction-part-3http-readme.md] A loop can iterate over these headers to inspect them.^[400-devops-09-scripting-language-golang-introduction-part-3http-readme.md] To add headers to the response, the w.Header().Add(key, value) method is utilized.^[400-devops-09-scripting-language-golang-introduction-part-3http-readme.md]
Handling HTTP methods¶
Servers must handle different HTTP methods, such as GET and POST, appropriately.
GET Requests¶
A GET request is typically used to retrieve data.^[400-devops-09-scripting-language-golang-introduction-part-3http-readme.md] In Go, this involves fetching data (e.g., from a struct slice), converting it to a format like JSON using json.Marshal, and writing it to the ResponseWriter.^[400-devops-09-scripting-language-golang-introduction-part-3http-readme.md]
POST Requests¶
A POST request is used to send data to the server for creation or updates.^[400-devops-09-scripting-language-golang-introduction-part-3http-readme.md]
To process a POST request:
- Validate Method: Check
r.Methodto ensure it isPOST. If not, return a405 Method Not Allowedstatus code usingw.WriteHeader(405).^[400-devops-09-scripting-language-golang-introduction-part-3http-readme.md] - Read Body: Read the incoming bytes from
r.Bodyusingioutil.ReadAll.^[400-devops-09-scripting-language-golang-introduction-part-3http-readme.md] - Decode Data: Unmarshal the bytes (e.g., from JSON) into a Go data structure.^[400-devops-09-scripting-language-golang-introduction-part-3http-readme.md]
- Error Handling: If unmarshaling fails (e.g., bad JSON), return a
400 Bad Requeststatus.^[400-devops-09-scripting-language-golang-introduction-part-3http-readme.md] - Process: Perform the necessary business logic (e.g., saving data to a file).^[400-devops-09-scripting-language-golang-introduction-part-3http-readme.md]
Related Concepts¶
- [[JSON marshaling]]
- [[Go standard library]]
- [[API design]]
Sources¶
^[400-devops-09-scripting-language-golang-introduction-part-3http-readme.md]