ways of doing HTTP requests
suggest changePackage net/http
has a layered design where each layer is a convenience wrapper on top of a lower layer.
Each lower layer is more complex but offers more control.
Here’s a recap of 3 ways of doing an HTTP GET request.
Use http.Get()
function
Using top-level http.Get()
function is the simplest but not recommended due to lack of timeouts.
Use http.Client.Get()
method
- create
*http.Client
with&http.Client{}
- set appropriate
Timeout
- use its
Get()
orPost()
orPostForm()
methods
Use http.Client.Do()
method
This allows the greatest control over the request.
- create
http.Client
and set apropriateTimeout
- create
*http.Request
withhttp.NewRequest
- set its
Method
to"GET"
,"POST"
,"HEAD"
,"PUT"
or"DELETE"
- set custom headers like
User-Agent
withHeader.Set(key, value string)
- set body to be sent by setting
Body
of typeio.ReadCloser
- set
TransferEncoding
- send the request with
client.Do(req *http.Request)
- set per-request timeout by creating a new request with context containing deadline
req = request.WithContext(ctx)
This is the only way to do PUT or DELETE requests.
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents