user1513388
user1513388

Reputation: 7441

Simple GoLang SSL example

Just started out with go with a Python background.

I need to make an SSL (HTTP GET and POST) connection to server with a self signed certificate and ignore (for now) the security errors.

This is dead easy in Python, but I can't seem to find a simple example.

Can somebody provide a really simple example.

UPDATE:

OK, so this is how it works, just need to work out how to disable the cert checking for now!

package main

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

func main() {
    response, err := http.Get( "https://golang.com/")
    if err != nil {
        fmt.Printf("%s", err)
        os.Exit(1)
    } else {
        defer response.Body.Close()
        contents, err := ioutil.ReadAll(response.Body)
        if err != nil {
            fmt.Printf("%s", err)
            os.Exit(1)
        }
        fmt.Printf("%s\n", string(contents))
    }
}

Upvotes: 1

Views: 2081

Answers (1)

user1513388
user1513388

Reputation: 7441

OK Sorted, here's what I did:

package main

import (
    "fmt"
    "net/http"
    "io/ioutil"
    "os"
    "crypto/tls"
)

func main() {

    tr := &http.Transport{
        TLSClientConfig: &tls.Config{InsecureSkipVerify : true},
    }

    client := &http.Client{Transport: tr}

    response, err := client.Get( "https://80.84.50.156/VEDsdk/")
    if err != nil {
        fmt.Printf("%s", err)
        os.Exit(1)
    } else {
        defer response.Body.Close()
        contents, err := ioutil.ReadAll(response.Body)
        if err != nil {
            fmt.Printf("%s", err)
            os.Exit(1)
        }
        fmt.Printf("%s\n", string(contents))
    }
}

Upvotes: 4

Related Questions