From f7f79f26006ef53cdadd2a0549e6379e38e86199 Mon Sep 17 00:00:00 2001 From: Andrew Ayer Date: Tue, 31 May 2022 15:37:47 -0400 Subject: [PATCH] logclient: buffer JSON request body This permits us to detect JSON marshalling errors, and makes it easy to retry the request. Request bodies are short so this should have negligible performance impact. --- ct/client/logclient.go | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/ct/client/logclient.go b/ct/client/logclient.go index a4d8802..4ec44eb 100644 --- a/ct/client/logclient.go +++ b/ct/client/logclient.go @@ -11,7 +11,6 @@ import ( "encoding/json" "errors" "fmt" - "io" "io/ioutil" "net/http" "net/url" @@ -124,12 +123,11 @@ func (c *LogClient) fetchAndParse(uri string, respBody interface{}) error { } func (c *LogClient) postAndParse(uri string, body interface{}, respBody interface{}) error { - bodyReader, bodyWriter := io.Pipe() - go func() { - json.NewEncoder(bodyWriter).Encode(body) - bodyWriter.Close() - }() - req, err := http.NewRequest("POST", uri, bodyReader) + bodyBytes, err := json.Marshal(body) + if err != nil { + return err + } + req, err := http.NewRequest("POST", uri, bytes.NewReader(bodyBytes)) if err != nil { return fmt.Errorf("POST %s: Sending request failed: %s", uri, err) }