Concurrent API Design - 6.4 Sending HTTP requests Flashcards
What are the steps involved in sending a request an reading a response using the http package?
- The
Do
method sends a request using aRequest
value. - It receives a response as a
Response
value. TheResponse
type represents a response from an HTTP request and contains details such as status code and body in a field calledBody
. - You can use the
Body
field to stream the server’s response.
What is io.Reader
?
The Reader
type is an interface that can be used to Read
a stream of bytes from any resource.
What is io.Writer
?
The Writer type is an interface that can be used to Write
a stream of bytes to any resource (such as file or memory).
What is io.Discard
?
Discard is a variable that implements Writer such that all Write calls succeed without doing anything.
What is io.Copy(dst, src)
?
Copies from src to dst
until either EOF is reached on src or an error occurs.
How many connections does http.DefaultClient
keep open and how many are allowed for reuse to the same host?
100 connections can be opened.
2 can be reused to send to the same host.
What is http.Transport
?
This is the Transport
type. The transport establishes TCP connections, sends HTTP requests, and has a connection pool to store idle connections.
What is the http.Client
?
The Client
type provides a nice API and allows you to send HTTP requests, handles cookies, and redirects.