Package client
Import Path:
net/http/client
Overview
The net/http/client
package provides the core functionality for making HTTP requests in Go. It abstracts away the complexities of network communication, allowing developers to send requests and receive responses with ease. This package is a fundamental part of the Go standard library for web development and interaction with APIs.
Key features include:
- Automatic handling of redirects.
- Support for connection pooling and reuse.
- TLS/SSL support.
- Customizable request and response handling.
- Integration with other Go packages for advanced scenarios.
Types
Client
A Client
represents a user-agent for making HTTP requests.
type Client struct { ... }
- Timeout
- If
Timeout
is non-zero, any request using this client that times out or is cancelled will be clamped to at most two successive calls tohttp.Transport.RoundTrip
. This may leave the request in an indeterminate state. - Transport
Transport
is used to handle all outgoing requests. If nil, the default transport is used.- CheckRedirect
CheckRedirect
, if not nil, specifies a function to call with a request that will be made if the response is a redirect. The function is passed the new request that is about to be made and the response that generated the exception. The return value of the function (either a Request or an error) determines if the redirect is allowed to proceed. If the function returns an error, the redirect is aborted and the error is returned to the caller ofDo
. If the function returns anil
Request
, the redirect is also aborted andnil
is returned to the caller ofDo
. Otherwise, the returnedRequest
is used for the redirect. The browser's default behavior is to follow redirects, so ifCheckRedirect
isnil
, this client will follow all redirects.- Jar
- Jar, if not nil, is a cookie jar. The Jar is used to persist cookies across requests.
Client.Do
Do
sends an HTTP request and returns an HTTP response.
func (c *Client) Do(req *http.Request) (*http.Response, error)
- req
- The request to send.
Returns:
*http.Response
: The HTTP response.
error
: An error, if any occurred.
Client.Get
Get
is a convenience wrapper around Client.Do
for making a GET request.
func (c *Client) Get(url string) (*http.Response, error)
- url
- The URL to fetch.
Returns:
*http.Response
: The HTTP response.
error
: An error, if any occurred.
Functions
NewClient
NewClient
returns a new Client
with sensible defaults.
func NewClient() *Client
Returns:
*Client
: A new HTTP client.
Examples
package main
import (
"fmt"
"net/http"
"net/http/client"
"io/ioutil"
)
func main() {
c := client.NewClient()
req, err := http.NewRequest("GET", "https://example.com", nil)
if err != nil {
fmt.Printf("Error creating request: %v\n", err)
return
}
resp, err := c.Do(req)
if err != nil {
fmt.Printf("Error making request: %v\n", err)
return
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Printf("Error reading response body: %v\n", err)
return
}
fmt.Printf("Status Code: %d\n", resp.StatusCode)
fmt.Printf("Response Body: %s\n", body)
}