net/http Flashcards

(14 cards)

1
Q

how is json encoded in the body of an http response?

A

a stream of bytes

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

how to decode json into a string?

A

var storage string
decoder := json.NewDecoder(res.Body)
if err := decoder.Decode(&storage); err != nil { return err, “” }

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

why and how should you explicity close a response body?

A

defer res.Body.Close()– to avoid memory issues. That said, net/http will handle tearing down the Transport layer connection for you.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

how does http.Get work under the hood?

A

it uses an http.DefaultClient

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

when would you prefer a Client over using http to make requests?

A

when you need control over redirect policies, or to change a header:
client := &http.Client{
CheckRedirect: redirectPolicyFunc,
}

resp, err := client.Get(“http://example.com”)
// …

req, err := http.NewRequest(“GET”, “http://example.com”, nil)
// …
req.Header.Add(“If-None-Match”, W/"wyzzy")
resp, err := client.Do(req)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

where can you configure connection settings (like proxies, TLS, keepalives, compression, etc)?

A

http.Transport

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

http.Transport.Proxy

A

Proxy specifies a function to return a proxy for a given
// Request. If the function returns a non-nil error, the
// request is aborted with the provided error.
//
// The proxy type is determined by the URL scheme. “http”,
// “https”, “socks5”, and “socks5h” are supported. If the scheme is empty,
// “http” is assumed.
// “socks5” is treated the same as “socks5h”.
//
// If the proxy URL contains a userinfo subcomponent,
// the proxy request will pass the username and password
// in a Proxy-Authorization header.
//
// If Proxy is nil or returns a nil *URL, no proxy is used.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

http.Transport.OnProxyConnectResponse

A

OPCR is called when the Transport gets an HTTP response from a proxy to CONNECT. It’s called before 200 OK is checked. If it returns an error, the request fails with that error

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

DialContext func(ctx context.Context, network, addr string) (net.Conn, error)

A

TCP connection. If it’s nil then package net is used. Runs concurrently with calls to RT. a RT call that starts a dial may reuse a previous connectoin if the earlier connection idles out before the later DC completes (connection reuse) (how tf does that work)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

When should you use Dial instead of DialContext?

A

trick question– the Dial function is deprecated

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

what’s a good alternative to the httptrace package?

A

otel is typically used for larger apps but httptrace is still useful for troubleshooting.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

tell me the top 3 things to know aobut func (c *Client) Do(req Request) (Response, error)

A
  • non-2xx status codes won’t return errors
  • If body isn’t both read to EOF AND closed, RTer may not be able to reuse connections for keepalives
  • 301-303 -> GET w no body; 307, 308 -> original http method and body, if req.GetBody is defined. NewReq auto-sets GB “for common standard library body types”
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q
A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q
A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly