My application is rejecting this, but when I curl the data it is working, so it seems there is somewhere that I am confused with how to compress this http payload in Go.
var buf bytes.Buffer
g := gzip.NewWriter(&buf)
g.Write([]byte("apples")
req, err := http.NewRequest("POST", q.host, bytes.NewReader(buf.Bytes()))
...
req.Header.Set("Content-Type", "text/plain")
req.Header.Set("Content-Encoding", "gzip")
resp, err := client.Do(req)
Does someone see where I am going wrong?
To compress the HTTP request body, you must attach the HTTP header indicating the sending of HTTP request body compressed in gzip format while sending the request message from the Web Service client. Implement the processing for attaching the HTTP header in the client application.
Copy all read bytes into the new file and close the fileNewWriter() from the compress/gzip package, we create a new gzip writer. Then we can use Write to write the compressed bytes in the file. After finishing, we need to close the file.
To compress a File in Golang, we use the gzip command. Then create a path to trace our file. Then leave the command to read our file.
Looks like the main issue is that I needed to close the gzip Writer:
b, err := batch.Json()
....
var buf bytes.Buffer
g := gzip.NewWriter(&buf)
if _, err = g.Write(b); err != nil {
slog.Error(err)
return
}
if err = g.Close(); err != nil {
slog.Error(err)
return
}
req, err := http.NewRequest("POST", q.host, &buf)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With