HTTP headers¶
HTTP headers play an important role in HTTP communication, functioning as metadata exchanged between a client and a server^[400-devops__09-Scripting-Language__golang__introduction__part-3.http__readme.md]. They are used to convey information such as content type, caching instructions, and authentication credentials^[400-devops__09-Scripting-Language__golang__introduction__part-3.http__readme.md].
Structure¶
In the net/http package (commonly used in [[Go]]), headers are structured as a dictionary (map) where the key is a string representing the header name, and the value is a slice of strings ([]string)^[400-devops__09-Scripting-Language__golang__introduction__part-3.http__readme.md].
Reading Headers¶
To access incoming headers on an [[HTTP]] request object r, one can iterate over the header map^[400-devops__09-Scripting-Language__golang__introduction__part-3.http__readme.md]. This allows the server to inspect metadata sent by the client.
for header, value := range r.Header {
fmt.Printf("Key: %v \t Value: %v \n", header, value)
}
Writing Headers¶
Headers can be added to the HTTP response using the Header().Add() method on the response writer^[400-devops__09-Scripting-Language__golang__introduction__part-3.http__readme.md].
w.Header().Add("TestHeader", "TestValue")
You can verify the response headers using tools like curl or browser development tools^[400-devops__09-Scripting-Language__golang__introduction__part-3.http__readme.md].
Sources¶
^[400-devops__09-Scripting-Language__golang__introduction__part-3.http__readme.md]