1
0
mirror of https://github.com/IBM/fp-go.git synced 2025-08-10 22:31:32 +02:00

fix: Content-Length header in Requester

Signed-off-by: Dr. Carsten Leue <carsten.leue@de.ibm.com>
This commit is contained in:
Dr. Carsten Leue
2023-12-16 22:06:40 +01:00
parent 96686425fb
commit b7ec18c83e

View File

@@ -17,11 +17,10 @@ package builder
import (
"bytes"
"io"
"net/http"
"net/url"
"strconv"
FL "github.com/IBM/fp-go/file"
F "github.com/IBM/fp-go/function"
IOE "github.com/IBM/fp-go/ioeither"
IOEH "github.com/IBM/fp-go/ioeither/http"
@@ -40,6 +39,7 @@ type (
body O.Option[IOE.IOEither[error, []byte]]
}
// BuilderBuilder returns a function that transforms a builder
BuilderBuilder = func(*Builder) *Builder
)
@@ -170,18 +170,18 @@ func (builder *Builder) AddHeaderHeader(name, value string) *Builder {
func (builder *Builder) Requester() IOEH.Requester {
return F.Pipe3(
builder.GetBody(),
O.Map(IOE.Map[error](F.Flow2(
bytes.NewReader,
FL.ToReader[*bytes.Reader],
))),
O.GetOrElse(F.Constant(IOE.Of[error, io.Reader](nil))),
IOE.Chain(func(rdr io.Reader) IOE.IOEither[error, *http.Request] {
O.Map(IOE.Map[error](bytes.NewReader)),
O.GetOrElse(F.Constant(IOE.Of[error, *bytes.Reader](nil))),
IOE.Chain(func(rdr *bytes.Reader) IOE.IOEither[error, *http.Request] {
return IOE.TryCatchError(func() (*http.Request, error) {
req, err := http.NewRequest(builder.GetMethod(), builder.GetUrl(), rdr)
if err == nil {
for name, value := range builder.GetHeaders() {
req.Header[name] = value
}
if rdr != nil {
req.Header.Set("Content-Length", strconv.FormatInt(rdr.Size(), 10))
}
}
return req, err
})