Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

how to add PKI certificate to a request #38

Closed
jschwinger233 opened this issue Jul 1, 2019 · 4 comments
Closed

how to add PKI certificate to a request #38

jschwinger233 opened this issue Jul 1, 2019 · 4 comments

Comments

@jschwinger233
Copy link

for client cert and trusted ca cert
thx

@imroc
Copy link
Owner

imroc commented Jul 5, 2019

req uses http.Client, you can customize the client, for example:

package main

import (
	"crypto/tls"
	"crypto/x509"
	"github.com/imroc/req"
	"io/ioutil"
	"log"
	"net/http"
)

const (
	localCertFile = "/usr/local/internal-ca/ca.crt"
)

func main() {
	// Get the SystemCertPool, continue with an empty pool on error
	rootCAs, _ := x509.SystemCertPool()
	if rootCAs == nil {
		rootCAs = x509.NewCertPool()
	}

	// Read in the cert file
	certs, err := ioutil.ReadFile(localCertFile)
	if err != nil {
		log.Fatalf("Failed to append %q to RootCAs: %v", localCertFile, err)
	}

	// Append our cert to the system pool
	if ok := rootCAs.AppendCertsFromPEM(certs); !ok {
		log.Println("No certs appended, using system certs only")
	}

	// Trust the augmented cert pool in our client
	config := &tls.Config{
		InsecureSkipVerify: false,
		RootCAs:            rootCAs,
	}
	tr := &http.Transport{TLSClientConfig: config}
	client := &http.Client{Transport: tr}

	req.SetClient(client)

}

@jschwinger233
Copy link
Author

jschwinger233 commented Jul 5, 2019

thx && understood, but given kennethreitz/requests can handle certs conveniently, shall we make the analogical APIs available?

@imroc
Copy link
Owner

imroc commented Jul 6, 2019

sounds great, I'll consider this

@imroc imroc closed this as completed Jan 28, 2022
@imroc
Copy link
Owner

imroc commented Jan 28, 2022

v2 support this, try and enjoy :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants