This library is provides a simple method for interacting with remote http servers.
This library does not have any non-standard requirements.
import "github.com/TheSp1der/httpclient"
import "log"
func main() {
x := httpclient.DefaultClient()
x.SetHeader("Accept", "application/json")
x.SetHeader("Content-Type", "application/json")
x.SetBasicAuth("username", "password")
x.SetPostData("{}")
output, err := x.Get("http://example.com")
if err != nil {
log.Println(err.Error())
}
log.Println(string(output))
}
import "github.com/TheSp1der/httpclient"
- type HTTPClient
- func DefaultClient() *HTTPClient
- func NewClient(httpClient *http.Client) *HTTPClient
- func (c *HTTPClient) Get(url string) ([]byte, error)
- func (c *HTTPClient) Patch(url string) ([]byte, error)
- func (c *HTTPClient) Post(url string) ([]byte, error)
- func (c *HTTPClient) Put(url string) ([]byte, error)
- func (c *HTTPClient) SetBasicAuth(username, password string) *HTTPClient
- func (c *HTTPClient) SetHeader(label string, value string) *HTTPClient
- func (c *HTTPClient) SetPostData(data string) *HTTPClient
type HTTPClient struct {
Client *http.Client
Data *bytes.Buffer
Headers map[string]string
Username string
Password string
}
HTTPClient is an interface for initializing the http client library.
func DefaultClient() *HTTPClient
DefaultClient is a function for defining a basic HTTP client with standard timeouts.
func NewClient(httpClient *http.Client) *HTTPClient
Create an HTTPClient with a user-provided net/http.Client
func (c *HTTPClient) Get(url string) ([]byte, error)
Get calls the net.http GET operation
func (c *HTTPClient) Patch(url string) ([]byte, error)
Patch calls the net.http PATCH operation
func (c *HTTPClient) Post(url string) ([]byte, error)
Post calls the net.http POST operation
func (c *HTTPClient) Put(url string) ([]byte, error)
Put calls the net.http PUT operation
func (c *HTTPClient) SetBasicAuth(username, password string) *HTTPClient
SetBasicAuth is a chaining function to set the username and password for basic authentication
func (c *HTTPClient) SetHeader(label string, value string) *HTTPClient
SetHeader is a chaining function to set arbitrary HTTP Headers
func (c *HTTPClient) SetPostData(data string) *HTTPClient
SetPostData is a chaining function to set POST/PUT/PATCH data