Welcome To Golang By Example

Set request headers for an outgoing HTTP request in Go (Golang)

Note: Related Post

Now let’s look at how we can set headers while making a HTTP request

Overview

Below is the format in which a Header is represented in go.

type Header map[string][]string

So the header is a key value pair with

Content-Type
Accept-Encoding

For eg if below headers are set in the outgoing request from a client then

user-agent: goapplication
foo: bar1
foo: bar2

Then at server the headers will look like

User-Agent: goapplication
Foo: bar1
Foo: bar2

Notice that:

Now we have seen how a header is represented in the request. Let’s see how we can set the request headers values when making HTTP requests. Assume we have the below key value pairs of headers which we have to set in the HTTP request

user-agent: goapplication
foo: bar1
foo: bar2

Header is present within the http.Request struct like as below.

type Request struct {
    Method string
    Header Header
    ...
}

In the below example let’s assume that variable r of type *http.Request type. Now let’s see different ways of setting a header

Using r.Header.Add() method

Below is the signature of the Add method

func (h Header) Add(key, value string)

This method is used to add the key value pair to the request header. We have already seen above that Header value can be array as well. So this method appends to the existing values that might already have been associated with the key.  Also the key will be converted to canonical form. For example if we add the foo header two times with different values

r.Header.Add("foo", "bar1")
r.Header.Add("foo", "bar2")

Then foo header will be set to [“bar1”, “bar2”]. Also foo will become Foo before making the http call

Using r.Header.Set method

Below is the signature of the function

func (h Header) Set(key, value string)

It can be used to set the header entries associated with the given key. Unlike the Add method, this method will replace any existing values associated with the key. Also the key will be converted to canonical form.

Example

Let’s see a program illustrating all the above points

package main
import (
    "fmt"
    "net/http"
    "time"
)
func main() {
    call("https://google.com", "GET")
}
func call(url, method string) error {
    client := &http.Client{
        Timeout: time.Second * 10,
    }
    req, err := http.NewRequest(method, url, nil)
    if err != nil {
        return fmt.Errorf("Got error %s", err.Error())
    }
    req.Header.Set("user-agent", "golang application")
    req.Header.Add("foo", "bar1")
    req.Header.Add("foo", "bar2")
    response, err := client.Do(req)
    if err != nil {
        return fmt.Errorf("Got error %s", err.Error())
    }
    defer response.Body.Close()
    return nil
}

Note in the above example how we are setting the headers while making the HTTP call

req.Header.Set("user-agent", "goapplication")
req.Header.Add("foo", "bar1")
req.Header.Add("foo", "bar2")