From 2fc300f4d968ad5e3053adbe14e2e8e8998794c7 Mon Sep 17 00:00:00 2001 From: CorentinB Date: Fri, 18 Dec 2020 21:32:37 +0100 Subject: [PATCH] Fix Content-Length not being written --- write.go | 28 ++++++++++++++++++---------- 1 file changed, 18 insertions(+), 10 deletions(-) diff --git a/write.go b/write.go index cc81788..cdc5912 100644 --- a/write.go +++ b/write.go @@ -72,14 +72,6 @@ func (w *Writer) WriteRecord(r *Record) (recordID string, err error) { return recordID, err } - // Write headers - for key, value := range r.Header { - _, err = io.WriteString(w.fileWriter, strings.Title(key)+": "+value+"\r\n") - if err != nil { - return recordID, err - } - } - // If PayloadPath isn't empty, it means that the payload we need to write // lives on disk if r.PayloadPath != "" { @@ -116,18 +108,34 @@ func (w *Writer) WriteRecord(r *Record) (recordID string, err error) { } } + // Write headers + r.Header["content-length"] = strconv.Itoa(contentLength) + for key, value := range r.Header { + _, err = io.WriteString(w.fileWriter, strings.Title(key)+": "+value+"\r\n") + if err != nil { + return recordID, err + } + } + _, err = io.WriteString(w.fileWriter, "\r\n\r\n") if err != nil { return recordID, err } - - r.Header["content-length"] = strconv.Itoa(contentLength) } else { data, err := ioutil.ReadAll(r.Content) if err != nil { return recordID, err } + // Write headers + r.Header["content-length"] = strconv.Itoa(len(data)) + for key, value := range r.Header { + _, err = io.WriteString(w.fileWriter, strings.Title(key)+": "+value+"\r\n") + if err != nil { + return recordID, err + } + } + _, err = io.WriteString(w.fileWriter, "\r\n"+string(data)+"\r\n\r\n") if err != nil { return recordID, err