Skip to content

net/http: Documentation of implementation details for clients #3359

Closed
@gopherbot

Description

@gopherbot

by elazarl:

In the net/http package there are three ways to send a request and receive a response.

The first one is, writing and reading the request directly from socket

The second one, using Transport.RoundTrip,

And the third is using Client.Do

The documentation is lacking as to what each of these is doing in edge cases.

For example, all will handle chunked encoding for you. The response.Body will never
return a non-chunked encoding you have to parse.

If you did not add directly Accept-Encoding:gzip, Transport is going to add that for
you, and will wrap the repsonse.Body with gzip.NewReader (sort of, it will also close
the original stream when closed).

Finally, client.Do, will also follow redirect responses (301 moved response will trigger
another request).

There may be more differences I didn't notice.

This behavior, kind-of makes sense, but it must be documented in the appropriate
functions.

See attached file for a demonstration of the different behaviours (note: leaks tcp
connection if using it too much). Run it with `go run sendreq.go` and see the response.
Change the code to discover more cases of different behavior.

Attachments:

  1. sendreq.go (1503 bytes)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions