Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

tls: no renegotiation error on HTTP request

Tags:

go

go-http

I'm trying to make a simple HTTP request in Go, after directly following the guides I keep getting the same error:

local error: tls: no renegotiation

I don't quite understand how to interpret this? I know it's not an issue on the server as when I call the same request from python it returns fine. Here's my code:

package main

import (
    "fmt"
    "net/http"
    "net/url"
    "strings"
    "time"
)

func main() {
    timeout := time.Duration(20 * time.Second)
    client := &http.Client{
        Timeout: timeout,
    }
    data := url.Values{
        "top":   {"10"},
        "lDate": {"2019-01-01"},
    }
    req, err := http.NewRequest("POST", "https://api.*********.com/AppAvailLoads?", strings.NewReader(data.Encode()))
    if err != nil {
        fmt.Println("Error in construction")
    }
    req.Header.Add("x-cdata-authtoken", "********")
    req.Header.Add("content-type", "application/x-www-form-urlencoded")
    resp, err := client.Do(req)
    if err != nil {
        fmt.Println("Error in request")
        fmt.Println(err)
    } else {
        fmt.Println(resp.Body)
        resp.Body.Close()
    }
}
like image 277
Adrian Coutsoftides Avatar asked Aug 08 '19 21:08

Adrian Coutsoftides


1 Answers

The solution was to to enable TLS renegotiation (go figure, I know), which is a constant that's part of the tls package as follows:

tr := &http.Transport{
    TLSClientConfig: &tls.Config{
        Renegotiation: tls.RenegotiateOnceAsClient,
        // You may need this if connecting to servers with self-signed certificates
        // InsecureSkipVerify: true,
    },
}

client := &http.Client{
    Timeout:   timeout,
    Transport: tr,
}

Which is weird, as no guides online explain this or show examples of how a common error such as local error: tls: no renegotiation occurs. I hope this is useful for people coming from other languages as it's not something one usually deals with!

like image 56
Adrian Coutsoftides Avatar answered Nov 01 '22 14:11

Adrian Coutsoftides