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