marcofranssen / terraform-provider-curl

Terraform provider that allows to make curl request to any http endpoint
MIT License
5 stars 3 forks source link

Add debug logging for the HTTP requests #5

Closed marcofranssen closed 2 years ago

marcofranssen commented 2 years ago

client.go

c.Transport = TeeRoundTripper{
    RoundTripper: c.Transport,
    Writer:       tfDebugLogWriter(),
}

// TeeRoundTripper copies request bodies to stdout.
type TeeRoundTripper struct {
    http.RoundTripper
    Writer io.Writer
}

// RoundTrip executes a single HTTP transaction, returning
// a Response for the provided Request.
func (t TeeRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
    fmt.Fprintf(t.Writer, "%s: %s ", req.Method, req.URL)
    if req.Body != nil {
        req.Body = struct {
            io.Reader
            io.Closer
        }{
            Reader: io.TeeReader(req.Body, t.Writer),
            Closer: req.Body,
        }
    }

    return t.RoundTripper.RoundTrip(req)
}
marcofranssen commented 2 years ago

Resolved by #7