How to get HTTP response without using the HTTP library in Golang?

1k Views Asked by At

I am trying to learn systems programming. I was wondering how I could place a GET request for a URL without using any libraries like HTTP for the same. Any help would be much appreciated!

1

There are 1 best solutions below

4
On

It's surprisingly easy if you're using sockets directly; HTTP is a very simple protocol. Here's an HTTP GET (with HTTP 1.1) to a given host at port 80:

package main

import (
    "fmt"
    "io/ioutil"
    "log"
    "net"
    "net/url"
    "os"
)

func main() {
    s := os.Args[1]
    u, err := url.Parse(s)
    if err != nil {
        log.Fatal(err)
    }

    conn, err := net.Dial("tcp", u.Host+":80")
    if err != nil {
        log.Fatal(err)
    }

    rt := fmt.Sprintf("GET %v HTTP/1.1\r\n", u.Path)
    rt += fmt.Sprintf("Host: %v\r\n", u.Host)
    rt += fmt.Sprintf("Connection: close\r\n")
    rt += fmt.Sprintf("\r\n")

    _, err = conn.Write([]byte(rt))
    if err != nil {
        log.Fatal(err)
    }

    resp, err := ioutil.ReadAll(conn)
    if err != nil {
        log.Fatal(err)
    }
    fmt.Println(string(resp))

    conn.Close()
}

That said, the whole HTTP protocol has many different options and variants and takes a lot of time, effort and tuning to implement.


The Go source code is fairly approachable usually, so you could read the code of net/http. There are also alternative HTTP packages implemented using lower layers, like https://github.com/valyala/fasthttp