make a http connect request in go

To make an HTTP connect request in Go, you can use the built-in net/http package. Here is an example:

main.go
package main

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

func main() {

    // create a client
    client := &http.Client{}

    // create a request object
    req, err := http.NewRequest("CONNECT", "https://www.google.com:443", nil)
    if err != nil {
        panic(err)
    }

    // execute the request
    resp, err := client.Do(req)
    if err != nil {
        panic(err)
    }

    // read the response body
    defer resp.Body.Close()
    body, err := ioutil.ReadAll(resp.Body)
    if err != nil {
        panic(err)
    }

    // print the response
    fmt.Println(string(body))
}
600 chars
36 lines

In this example, we create an HTTP client and a request object with the http.NewRequest function. We set the request method to "CONNECT" and the URL to "https://www.google.com:443". Then we execute the request with client.Do(req) and read the response body using the ioutil.ReadAll(resp.Body) function. Finally, we print the response as a string with fmt.Println(string(body)).

related categories

gistlibby LogSnag