Skip to content

Commit

Permalink
fix: Content-Length header in Requester
Browse files Browse the repository at this point in the history
Signed-off-by: Dr. Carsten Leue <carsten.leue@de.ibm.com>
  • Loading branch information
CarstenLeue committed Dec 16, 2023
1 parent 9668642 commit b7ec18c
Showing 1 changed file with 8 additions and 8 deletions.
16 changes: 8 additions & 8 deletions ioeither/http/builder/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -40,6 +39,7 @@ type (
body O.Option[IOE.IOEither[error, []byte]]
}

// BuilderBuilder returns a function that transforms a builder
BuilderBuilder = func(*Builder) *Builder
)

Expand Down Expand Up @@ -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
})
Expand Down

0 comments on commit b7ec18c

Please sign in to comment.