Skip to content

request.GetBody() breaks the request object #130

@bcomnes

Description

@bcomnes

I tracked down a bug in the implementation of request.GetBody I think.

I wrote a failing test to demonstrate the bug:

https://gist.github.com/bcomnes/4ffddcd270e8721267efa4b9e2ed89b8#file-http_test-go

The example is pretty contrived, but if you use a custom roundTripper and get ahold of the req object generated by go-openapi before you give it to transport.RoundTrup and call req.GetBody, it causes the client operation ClientRequestWriterFunc to write out any BodyParam to the req.Body again, doubling/tripling the req.Body etc. The length of the req.Body basically grows to n x req.ContentLength where n is the number of times you call req.GetBody before reading req.Body.

At this point the req object is toast, since the length of the Body no longer matches the ContentLengh. Passing it to transport.RoundTrip fails with this error:

'https://github.com/golang/go/blob/61170f85e62f1326d42c4dbd8aa17ab4a1305a87/src/net/http/transfer.go#L387-L389

The contents of the Body returned by req.GetBody is perfect, even if you call req.GetBody multiple times. Its only req.Body that is affected. This only applies when you have body parameters.

Bug

Calling .GetBody() should not cause the Body to be written to again by ClientRequestWriterFunc

Possible resolution

If you have any ideas of where this is happening, I would love to hear them. I would like to PR the failing test for you, and start hunting down an actual fix. I am open to any suggestions if you have them.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions