README
¶
httpreq
httpreq
is an http request library written with Golang to make requests and handle responses easily.
Install
go get github.com/binalyze/httpreq
Overview
httpreq
implements a friendly API over Go's existing net/http
library.
Req
and Response
are two most important struct. You can think of Req
as a client that initiate HTTP requests, Resp
as a information container for the request and response. They all provide simple and convenient APIs that allows you to do a lot of things.
req := httpreq.New(url)
resp, err := req.Get()
Roadmap
- Support query parameters
- Support cookies
- Support XML
- Support proxy
- Configurable transport
Usage
Here is an example to use some helper methods of httpreq
. You can find more examples in test files.
Request
// Create new request
req := httpreq.New("https://your-address-to-send-json.com")
// Set Timeout
req.SetTimeout(30 * time.Second)
// Set Header (i.e. JWT Bearer Token)
var bearer = "Bearer " + <ACCESS TOKEN HERE>
req.SetHeaders(map[string]string{"Authorization": bearer})
// Set Content Type
req.SetContentType("application/json")
// Set JSON raw body
info := &Data{
FirstName: "John",
LastName: "Doe",
Age: 42,
}
jsonData, _ := json.Marshal(info)
req.SetBody(jsonData)
// Send Request
resp, _ := req.Post()
Response
// Body
result, err := resp.Body()
// Original response
response := resp.Response()
// StatusCode
statusCode := resp.StatusCode()
// Headers
headers := resp.Headers()
Documentation
¶
Index ¶
- type BuiltinLogger
- func (l *BuiltinLogger) Debug(args ...interface{})
- func (l *BuiltinLogger) Debugf(format string, args ...interface{})
- func (l *BuiltinLogger) Error(args ...interface{})
- func (l *BuiltinLogger) Errorf(format string, args ...interface{})
- func (l *BuiltinLogger) Fatal(args ...interface{})
- func (l *BuiltinLogger) Fatalf(format string, args ...interface{})
- func (l *BuiltinLogger) Info(args ...interface{})
- func (l *BuiltinLogger) Infof(format string, args ...interface{})
- func (l *BuiltinLogger) Warn(args ...interface{})
- func (l *BuiltinLogger) Warnf(format string, args ...interface{})
- type Logger
- type Req
- func (r *Req) Delete() (*Response, error)
- func (r *Req) Get() (*Response, error)
- func (r *Req) Post() (*Response, error)
- func (r *Req) PostJSON() (*Response, error)
- func (r *Req) Put() (*Response, error)
- func (r *Req) SetBody(data []byte) *Req
- func (r *Req) SetBodyXML() *Req
- func (r *Req) SetContentType(contentType string) *Req
- func (r *Req) SetCookie(c *http.Cookie) *Req
- func (r *Req) SetForm(files []map[string]string, fields []map[string]string) *Req
- func (r *Req) SetHeaders(headers map[string]string) *Req
- func (r *Req) SetParam(param, value string) *Req
- func (r *Req) SetParams(queryParams map[string]string) *Req
- func (r *Req) SetProxy(u string) *Req
- func (r *Req) SetTLSConfig(c *tls.Config) *Req
- func (r *Req) SetTimeout(d time.Duration) *Req
- func (r *Req) SetTransport(transport *http.Transport) *Req
- type Response
- func (r *Response) Body() ([]byte, error)
- func (r *Response) Close() error
- func (r *Response) DownloadFile(downloadDir string) (contentType string, filePath string, err error)
- func (r *Response) Headers() http.Header
- func (r *Response) Response() *http.Response
- func (r *Response) SaveFile(filePath string) error
- func (r *Response) StatusCode() int
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type BuiltinLogger ¶
type BuiltinLogger struct {
// contains filtered or unexported fields
}
func NewBuiltinLogger ¶
func NewBuiltinLogger() *BuiltinLogger
func (*BuiltinLogger) Debug ¶
func (l *BuiltinLogger) Debug(args ...interface{})
func (*BuiltinLogger) Debugf ¶
func (l *BuiltinLogger) Debugf(format string, args ...interface{})
func (*BuiltinLogger) Error ¶
func (l *BuiltinLogger) Error(args ...interface{})
func (*BuiltinLogger) Errorf ¶
func (l *BuiltinLogger) Errorf(format string, args ...interface{})
func (*BuiltinLogger) Fatal ¶
func (l *BuiltinLogger) Fatal(args ...interface{})
func (*BuiltinLogger) Fatalf ¶
func (l *BuiltinLogger) Fatalf(format string, args ...interface{})
func (*BuiltinLogger) Info ¶
func (l *BuiltinLogger) Info(args ...interface{})
func (*BuiltinLogger) Infof ¶
func (l *BuiltinLogger) Infof(format string, args ...interface{})
func (*BuiltinLogger) Warn ¶
func (l *BuiltinLogger) Warn(args ...interface{})
func (*BuiltinLogger) Warnf ¶
func (l *BuiltinLogger) Warnf(format string, args ...interface{})
type Logger ¶
type Logger interface { Debug(args ...interface{}) Debugf(format string, args ...interface{}) Info(args ...interface{}) Infof(format string, args ...interface{}) Warn(args ...interface{}) Warnf(format string, args ...interface{}) Error(args ...interface{}) Errorf(format string, args ...interface{}) Fatal(args ...interface{}) Fatalf(format string, args ...interface{}) }
type Req ¶
Req is main struct for requests
func (*Req) SetContentType ¶
SetContentType sets content type of request
func (*Req) SetHeaders ¶
SetHeaders sets request headers
func (*Req) SetTLSConfig ¶
SetTLSConfig changes the request TLS client configuration
func (*Req) SetTimeout ¶
SetTimeout changes the request timeout
type Response ¶
type Response struct {
// contains filtered or unexported fields
}
Response is the main struct which holds the http.Response and data.
func (*Response) DownloadFile ¶
func (r *Response) DownloadFile(downloadDir string) (contentType string, filePath string, err error)
DownloadFile looks for Content-Disposition header to find the filename attribute and returns the content-type header with saved file path that is saved under given downloadDir.
func (*Response) StatusCode ¶
StatusCode returns the status code of the response