ZetCode

Go HTTP GET/POST request

last modified July 9, 2020

Go GET/POST request tutorial shows how to send HTTP GET and POST requests in golang.

HTTP

The Hypertext Transfer Protocol (HTTP) is an application protocol for distributed, collaborative, hypermedia information systems. HTTP protocol is the foundation of data communication for the World Wide Web.

In the examples, we use httpbin.org, which is a freely available HTTP request and response service, and the webcode.me, which is a tiny HTML page for testing.

HTTP GET

The HTTP GET method requests a representation of the specified resource. Requests using GET should only retrieve data.

HTTP POST

The HTTP POST method sends data to the server. It is often used when uploading a file or when submitting a completed web form.

Go http

In Go, we use the http package to create GET and POST requests. The package provides HTTP client and server implementations.

Go GET request

The following example creates a simple GET request in Go.

get_request.go
package main

import (
    "fmt"
    "io/ioutil"
    "net/http"
)

func main() {

    resp, err := http.Get("http://webcode.me")

    if err != nil {
        panic(err)
    }

    defer resp.Body.Close()

    body, err := ioutil.ReadAll(resp.Body)

    if err != nil {

        panic(err)
    }

    fmt.Println(string(body))
}

We create a GET request to the webcode.me webpage.

resp, err := http.Get("http://webcode.me")

A get request is issued with the Get function.

if err != nil {
    panic(err)
}

We check for the error.

defer resp.Body.Close()

The client must close the response body when finished.

body, err := ioutil.ReadAll(resp.Body)

We read the content of the body with ReadAll.

fmt.Println(string(body))

We print the received data to the console.

$ go run get_request.go
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>My html page</title>
</head>
<body>

    <p>
        Today is a beautiful day. We go swimming and fishing.
    </p>
    
    <p>
         Hello there. How are you?
    </p>
    
</body>
</html>

This is the output.

Go HTTP POST request

The next example creates a POST request.

post_request.go
package main

import (
    "encoding/json"
    "fmt"
    "net/http"
    "net/url"
)

func main() {

    data := url.Values {
        "name": {"John Doe"},
        "occupation": {"gardener"},
    }

    resp, err := http.PostForm("https://httpbin.org/post", data)

    if err != nil {
        panic(err)
    }

    var res map[string]interface{}

    json.NewDecoder(resp.Body).Decode(&res)

    fmt.Println(res["form"])
}

We send a POST request to the https://httpbin.org/post page.

data := url.Values {
    "name": {"John Doe"},
    "occupation": {"gardener"},
}

The data is in JSON format.

resp, err := http.PostForm("https://httpbin.org/post", data)

The data is sent with PostForm function.

var res map[string]interface{}

json.NewDecoder(resp.Body).Decode(&res)

We decode the reqponse body into a map.

fmt.Println(res["form"])

We print the received data.

$ go run post_request.go 
map[name:John Doe occupation:gardener]

This is the output.

In this tutorial, we have created a GET and a POST request in Go.

List all Go tutorials.