http.Client.Do(req) vs. transport.RoundTrip(req)

3,006 views
Skip to first unread message

Gyu-Ho Lee

unread,
Nov 3, 2014, 3:37:38 PM11/3/14
to golan...@googlegroups.com
I saw Andrew using RoundTrip in his code (https://github.com/nf/dl/blob/master/dl.go#L165). 
And have been trying to understand when to use RoundTrip and when to use Client.Do.

I am running the following code in my local machine:
http://play.golang.org/p/x6E0T1Hyfz

client, transport := NewClient()
req, err := http.NewRequest("HEAD", "http://google.com", nil)
transport.RoundTrip(req)

And  `RoundTrip` always takes much less for `HEAD` request to http://google.com

301 Moved Permanently
RoundTrip took: 51.102069ms

200 OK
Client took: 129.125018ms


1. RoundTrip takes less time to finish.
Is it because RoundTrip makes single HTTP transaction(http://golang.org/src/pkg/net/http/client.go)?

2. What do we mean by `RoundTrip should not attempt to interpret the response.`? 
Then when do we use RoundTripper?

3. Why does RoundTrip return 301 instead of 200?

Guess I am just confused about RoundTripper in general.

Thanks in advance.

James Bardin

unread,
Nov 3, 2014, 3:46:16 PM11/3/14
to golan...@googlegroups.com


On Monday, November 3, 2014 3:37:38 PM UTC-5, Gyu-Ho Lee wrote:


1. RoundTrip takes less time to finish.
Is it because RoundTrip makes single HTTP transaction(http://golang.org/src/pkg/net/http/client.go)?


Yes. If you get a 3xx response, it will take multiple requests.

 
2. What do we mean by `RoundTrip should not attempt to interpret the response.`? 
Then when do we use RoundTripper?


Use RoundTripper when the client doesn't provide what you need for some reason. Generally the Client is what you want.

 
3. Why does RoundTrip return 301 instead of 200?


For the reason in #2 -- it can't interpret the response. The Client is what handles redirects for you

 
Guess I am just confused about RoundTripper in general.



Read through the client code and wee what else it provides on top of RoundTripper. 

Andrew Gerrand

unread,
Nov 3, 2014, 4:26:07 PM11/3/14
to James Bardin, golan...@googlegroups.com
James is right. The Client is more appropriate in most cases.

In my program I didn't need to follow redirects nor handle cookies, so I went straight for http.DefaultTransport.

If in doubt, read the code.

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Andrew Gerrand

unread,
Nov 3, 2014, 4:27:27 PM11/3/14
to Gyu-Ho Lee, golan...@googlegroups.com
On Tue Nov 04 2014 at 7:37:44 AM Gyu-Ho Lee <gyuh...@gmail.com> wrote:
2. What do we mean by `RoundTrip should not attempt to interpret the response.`? 
Then when do we use RoundTripper?

Those comments are for *implementers* of the RoundTripper interface. A RoundTripper should make a request and return the response; that's all.

Andrew 

Gyu-Ho Lee

unread,
Nov 3, 2014, 5:14:17 PM11/3/14
to Andrew Gerrand, golan...@googlegroups.com
Now I understand more. Very much appreciate your answers!

--
You received this message because you are subscribed to a topic in the Google Groups "golang-nuts" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/golang-nuts/wsxcyMzYOtI/unsubscribe.
To unsubscribe from this group and all its topics, send an email to golang-nuts...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages