How to send a https request with a certificate golang


I have a server which has a rest API running over https. I want to make a call to this rest api in my application which is running in different port but since this is over https I am getting

Post https://localhost:8080/api/v1/myapi: x509: certificate signed by unknown authority

I have 2 files pulic_key.pem and private_key which can used to verify the certificate. How can verify certificate while sending rest request using golang? I am using &http.Client{} to send a rest request. Here is what I am doing to ignore the certificate right now.

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

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


You need to add CA of your certificate to your transport like:

package main

import (

func main() {
    caCert, err := ioutil.ReadFile("rootCA.crt")
    if err != nil {
    caCertPool := x509.NewCertPool()

    client := &http.Client{
        Transport: &http.Transport{
            TLSClientConfig: &tls.Config{
                RootCAs:      caCertPool,

    _, err := client.Get("")
    if err != nil {

But I guess you just haven’t created CA to make your certificates. Here is the list of commands without explanation which can help you to make certificates signed with your own CA. For more information, you can Google it.

  1. Generating CA

    openssl genrsa -out rootCA.key 4096
    openssl req -x509 -new -key rootCA.key -days 3650 -out rootCA.crt
  2. Generate certificate for signed with created CA

    openssl genrsa -out 2048
    openssl req -new -key -out
    #In answer to question `Common Name (e.g. server FQDN or YOUR name) []:` you should set `` (your real domain name)
    openssl x509 -req -in -CA rootCA.crt -CAkey rootCA.key -CAcreateserial -days 365 -out

Answered By – CrazyCrow

Answer Checked By – Dawn Plyler (GoLangFix Volunteer)

Leave a Reply

Your email address will not be published.