requests

package module
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 15, 2019 License: Apache-2.0 Imports: 18 Imported by: 0

README

requests

基于Golang的HTTP请求封装

使用

go get github.com/lifei6671/requests
GET请求
resp, err := requests.NewHttpRequest("GET", "https://www.baidu.com/s").
	WithParam("wd", "golang").
	WithHeader("Referer", "https://www.baidu.com").
	WithHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8").
	WithHeader("Accept-Language", "zh-CN,zh;q=0.9,en;q=0.8,zh-TW;q=0.7").
	WithHeader("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.80 Safari/537.36").
	WithDebug(true).
	GetHttpResponse()
if err != nil {
	fmt.Println(err)
} else {
	body,err := resp.Bytes()
}
POST 请求
body := []byte("www.baidu.com")
resp, err := requests.NewHttpRequest( "POST", "https://www.baidu.com/").WithBody(body).GetHttpResponse()
if err != nil {
	fmt.Println(err)
} else {
	body,err := resp.String()
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Files

type Files map[string]string

type HttpRequest

type HttpRequest struct {
	// contains filtered or unexported fields
}

func NewHttpRequest

func NewHttpRequest(method, rawUrl string) *HttpRequest

func (*HttpRequest) Error

func (r *HttpRequest) Error() error

func (*HttpRequest) Errors

func (r *HttpRequest) Errors() []error

func (*HttpRequest) GetHttpResponse

func (r *HttpRequest) GetHttpResponse() (*HttpResponse, error)

func (*HttpRequest) GetRequest

func (r *HttpRequest) GetRequest() *http.Request

func (*HttpRequest) WidthContentType

func (r *HttpRequest) WidthContentType(contentType string) *HttpRequest

func (*HttpRequest) WithBasicAuth

func (r *HttpRequest) WithBasicAuth(username, password string) *HttpRequest

func (*HttpRequest) WithBody

func (r *HttpRequest) WithBody(data interface{}) *HttpRequest

func (*HttpRequest) WithConnectTimeout

func (r *HttpRequest) WithConnectTimeout(timeout time.Duration) *HttpRequest

func (*HttpRequest) WithContext

func (r *HttpRequest) WithContext(ctx context.Context) *HttpRequest

func (*HttpRequest) WithCookie

func (r *HttpRequest) WithCookie(cookie *http.Cookie) *HttpRequest

func (*HttpRequest) WithDebug

func (r *HttpRequest) WithDebug(isDebug bool) *HttpRequest

func (*HttpRequest) WithFile

func (r *HttpRequest) WithFile(fileName, filePath string) *HttpRequest

func (*HttpRequest) WithFiles

func (r *HttpRequest) WithFiles(files Files) *HttpRequest

func (*HttpRequest) WithHeader

func (r *HttpRequest) WithHeader(key string, value string) *HttpRequest

func (*HttpRequest) WithHeaders

func (r *HttpRequest) WithHeaders(headers map[string]string) *HttpRequest

func (*HttpRequest) WithHost

func (r *HttpRequest) WithHost(host string) *HttpRequest

func (*HttpRequest) WithJson

func (r *HttpRequest) WithJson(v interface{}) *HttpRequest

func (*HttpRequest) WithParam

func (r *HttpRequest) WithParam(key string, value string) *HttpRequest

func (*HttpRequest) WithParams

func (r *HttpRequest) WithParams(params url.Values) *HttpRequest

func (*HttpRequest) WithPrint

func (r *HttpRequest) WithPrint(fn func(v ...interface{})) *HttpRequest

func (*HttpRequest) WithProtocolVersion

func (r *HttpRequest) WithProtocolVersion(vers string) *HttpRequest

func (*HttpRequest) WithProxy

func (r *HttpRequest) WithProxy(proxy func(*http.Request) (*url.URL, error)) *HttpRequest

func (*HttpRequest) WithReadWriteTimeout

func (r *HttpRequest) WithReadWriteTimeout(timeout time.Duration) *HttpRequest

func (*HttpRequest) WithRedirect

func (r *HttpRequest) WithRedirect(redirect func(req *http.Request, via []*http.Request) error) *HttpRequest

func (*HttpRequest) WithReferer

func (r *HttpRequest) WithReferer(referer string) *HttpRequest

func (*HttpRequest) WithSetting

func (r *HttpRequest) WithSetting(setting HttpRequestSetting) *HttpRequest

func (*HttpRequest) WithTLSConfig

func (r *HttpRequest) WithTLSConfig(config *tls.Config) *HttpRequest

func (*HttpRequest) WithTransport

func (r *HttpRequest) WithTransport(transport http.RoundTripper) *HttpRequest

func (*HttpRequest) WithUserAgent

func (r *HttpRequest) WithUserAgent(ua string) *HttpRequest

type HttpRequestSetting

type HttpRequestSetting struct {
	UserAgent        string
	ConnectTimeout   time.Duration
	ReadWriteTimeout time.Duration
	TLSClientConfig  *tls.Config
	Proxy            func(*http.Request) (*url.URL, error)
	Transport        http.RoundTripper
	EnableCookie     bool
	Gzip             bool
	Retry            int
	CheckRedirect    func(req *http.Request, via []*http.Request) error
}

type HttpResponse

type HttpResponse struct {
	http.Response
	// contains filtered or unexported fields
}

func Delete

func Delete(rawUrl string) (*HttpResponse, error)

func Get

func Get(rawUrl string) (*HttpResponse, error)
func Head(rawUrl string) (*HttpResponse, error)

func Post

func Post(rawUrl string, body []byte) (*HttpResponse, error)

func PostForm

func PostForm(rawUrl string, body url.Values) (*HttpResponse, error)

func (*HttpResponse) Bytes

func (r *HttpResponse) Bytes() ([]byte, error)

func (*HttpResponse) SaveFile

func (r *HttpResponse) SaveFile(filename string) error

func (*HttpResponse) String

func (r *HttpResponse) String() (string, error)

func (*HttpResponse) ToJson

func (r *HttpResponse) ToJson(v interface{}) error

func (*HttpResponse) ToXml

func (r *HttpResponse) ToXml(v interface{}) error

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL