httplib

package
Version: v0.0.0-...-b0ab836 Latest Latest
Warning

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

Go to latest
Published: Aug 21, 2018 License: Apache-2.0 Imports: 18 Imported by: 0

README

httplib

httplib is an libs help you to curl remote url.

How to use?

GET

you can use Get to crawl data.

import "github.com/izi-global/izigo/httplib"

str, err := httplib.Get("http://go.izi.asia/").String()
if err != nil {
        // error
}
fmt.Println(str)

POST

POST data to remote url

req := httplib.Post("http://go.izi.asia/")
req.Param("username","diepdt")
req.Param("password","123456")
str, err := req.String()
if err != nil {
        // error
}
fmt.Println(str)

Set timeout

The default timeout is 60 seconds, function prototype:

SetTimeout(connectTimeout, readWriteTimeout time.Duration)

Example:

// GET
httplib.Get("http://go.izi.asia/").SetTimeout(100 * time.Second, 30 * time.Second)

// POST
httplib.Post("http://go.izi.asia/").SetTimeout(100 * time.Second, 30 * time.Second)

Debug

If you want to debug the request info, set the debug on

httplib.Get("http://go.izi.asia/").Debug(true)

Set HTTP Basic Auth

str, err := Get("http://go.izi.asia/").SetBasicAuth("user", "passwd").String()
if err != nil {
        // error
}
fmt.Println(str)

Set HTTPS

If request url is https, You can set the client support TSL:

httplib.SetTLSClientConfig(&tls.Config{InsecureSkipVerify: true})

More info about the tls.Config please visit http://golang.org/pkg/crypto/tls/#Config

Set HTTP Version

some servers need to specify the protocol version of HTTP

httplib.Get("http://go.izi.asia/").SetProtocolVersion("HTTP/1.1")

some http request need setcookie. So set it like this:

cookie := &http.Cookie{}
cookie.Name = "username"
cookie.Value  = "diepdt"
httplib.Get("http://go.izi.asia/").SetCookie(cookie)

Upload file

httplib support mutil file upload, use req.PostFile()

req := httplib.Post("http://go.izi.asia/")
req.Param("username","diepdt")
req.PostFile("uploadfile1", "httplib.pdf")
str, err := req.String()
if err != nil {
        // error
}
fmt.Println(str)

See godoc for further documentation and examples.

Documentation

Overview

Package httplib is used as http.Client Usage:

import "github.com/izi-global/izigo/httplib"

	b := httplib.Post("http://go.izi.asia/")
	b.Param("username","diepdt")
	b.Param("password","123456")
	b.PostFile("uploadfile1", "httplib.pdf")
	b.PostFile("uploadfile2", "httplib.txt")
	str, err := b.String()
	if err != nil {
		t.Fatal(err)
	}
	fmt.Println(str)

 more docs http://go.izi.asia/docs/module/httplib.md

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func SetDefaultSetting

func SetDefaultSetting(setting IZIGoHTTPSettings)

SetDefaultSetting Overwrite default settings

func TimeoutDialer

func TimeoutDialer(cTimeout time.Duration, rwTimeout time.Duration) func(net, addr string) (c net.Conn, err error)

TimeoutDialer returns functions of connection dialer with timeout settings for http.Transport Dial field.

Types

type IZIGoHTTPRequest

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

IZIGoHTTPRequest provides more useful methods for requesting one url than http.Request.

func Delete

func Delete(url string) *IZIGoHTTPRequest

Delete returns *IZIGoHttpRequest DELETE method.

func Get

func Get(url string) *IZIGoHTTPRequest

Get returns *IZIGoHttpRequest with GET method.

func Head(url string) *IZIGoHTTPRequest

Head returns *IZIGoHttpRequest with HEAD method.

func NewIZIGoRequest

func NewIZIGoRequest(rawurl, method string) *IZIGoHTTPRequest

NewIZIGoRequest return *IZIGoHttpRequest with specific method

func Post

func Post(url string) *IZIGoHTTPRequest

Post returns *IZIGoHttpRequest with POST method.

func Put

func Put(url string) *IZIGoHTTPRequest

Put returns *IZIGoHttpRequest with PUT method.

func (*IZIGoHTTPRequest) Body

func (b *IZIGoHTTPRequest) Body(data interface{}) *IZIGoHTTPRequest

Body adds request raw body. it supports string and []byte.

func (*IZIGoHTTPRequest) Bytes

func (b *IZIGoHTTPRequest) Bytes() ([]byte, error)

Bytes returns the body []byte in response. it calls Response inner.

func (*IZIGoHTTPRequest) Debug

func (b *IZIGoHTTPRequest) Debug(isdebug bool) *IZIGoHTTPRequest

Debug sets show debug or not when executing request.

func (*IZIGoHTTPRequest) DoRequest

func (b *IZIGoHTTPRequest) DoRequest() (resp *http.Response, err error)

DoRequest will do the client.Do

func (*IZIGoHTTPRequest) DumpBody

func (b *IZIGoHTTPRequest) DumpBody(isdump bool) *IZIGoHTTPRequest

DumpBody setting whether need to Dump the Body.

func (*IZIGoHTTPRequest) DumpRequest

func (b *IZIGoHTTPRequest) DumpRequest() []byte

DumpRequest return the DumpRequest

func (*IZIGoHTTPRequest) GetRequest

func (b *IZIGoHTTPRequest) GetRequest() *http.Request

GetRequest return the request object

func (*IZIGoHTTPRequest) Header

func (b *IZIGoHTTPRequest) Header(key, value string) *IZIGoHTTPRequest

Header add header item string in request.

func (*IZIGoHTTPRequest) JSONBody

func (b *IZIGoHTTPRequest) JSONBody(obj interface{}) (*IZIGoHTTPRequest, error)

JSONBody adds request raw body encoding by JSON.

func (*IZIGoHTTPRequest) Param

func (b *IZIGoHTTPRequest) Param(key, value string) *IZIGoHTTPRequest

Param adds query param in to request. params build query string as ?key1=value1&key2=value2...

func (*IZIGoHTTPRequest) PostFile

func (b *IZIGoHTTPRequest) PostFile(formname, filename string) *IZIGoHTTPRequest

PostFile add a post file to the request

func (*IZIGoHTTPRequest) Response

func (b *IZIGoHTTPRequest) Response() (*http.Response, error)

Response executes request client gets response mannually.

func (*IZIGoHTTPRequest) Retries

func (b *IZIGoHTTPRequest) Retries(times int) *IZIGoHTTPRequest

Retries sets Retries times. default is 0 means no retried. -1 means retried forever. others means retried times.

func (*IZIGoHTTPRequest) SetBasicAuth

func (b *IZIGoHTTPRequest) SetBasicAuth(username, password string) *IZIGoHTTPRequest

SetBasicAuth sets the request's Authorization header to use HTTP Basic Authentication with the provided username and password.

func (*IZIGoHTTPRequest) SetCheckRedirect

func (b *IZIGoHTTPRequest) SetCheckRedirect(redirect func(req *http.Request, via []*http.Request) error) *IZIGoHTTPRequest

SetCheckRedirect specifies the policy for handling redirects.

If CheckRedirect is nil, the Client uses its default policy, which is to stop after 10 consecutive requests.

func (*IZIGoHTTPRequest) SetCookie

func (b *IZIGoHTTPRequest) SetCookie(cookie *http.Cookie) *IZIGoHTTPRequest

SetCookie add cookie into request.

func (*IZIGoHTTPRequest) SetEnableCookie

func (b *IZIGoHTTPRequest) SetEnableCookie(enable bool) *IZIGoHTTPRequest

SetEnableCookie sets enable/disable cookiejar

func (*IZIGoHTTPRequest) SetHost

func (b *IZIGoHTTPRequest) SetHost(host string) *IZIGoHTTPRequest

SetHost set the request host

func (*IZIGoHTTPRequest) SetProtocolVersion

func (b *IZIGoHTTPRequest) SetProtocolVersion(vers string) *IZIGoHTTPRequest

SetProtocolVersion Set the protocol version for incoming requests. Client requests always use HTTP/1.1.

func (*IZIGoHTTPRequest) SetProxy

func (b *IZIGoHTTPRequest) SetProxy(proxy func(*http.Request) (*url.URL, error)) *IZIGoHTTPRequest

SetProxy set the http proxy example:

func(req *http.Request) (*url.URL, error) {
	u, _ := url.ParseRequestURI("http://127.0.0.1:8118")
	return u, nil
}

func (*IZIGoHTTPRequest) SetTLSClientConfig

func (b *IZIGoHTTPRequest) SetTLSClientConfig(config *tls.Config) *IZIGoHTTPRequest

SetTLSClientConfig sets tls connection configurations if visiting https url.

func (*IZIGoHTTPRequest) SetTimeout

func (b *IZIGoHTTPRequest) SetTimeout(connectTimeout, readWriteTimeout time.Duration) *IZIGoHTTPRequest

SetTimeout sets connect time out and read-write time out for IZIGoRequest.

func (*IZIGoHTTPRequest) SetTransport

func (b *IZIGoHTTPRequest) SetTransport(transport http.RoundTripper) *IZIGoHTTPRequest

SetTransport set the setting transport

func (*IZIGoHTTPRequest) SetUserAgent

func (b *IZIGoHTTPRequest) SetUserAgent(useragent string) *IZIGoHTTPRequest

SetUserAgent sets User-Agent header field

func (*IZIGoHTTPRequest) Setting

Setting Change request settings

func (*IZIGoHTTPRequest) String

func (b *IZIGoHTTPRequest) String() (string, error)

String returns the body string in response. it calls Response inner.

func (*IZIGoHTTPRequest) ToFile

func (b *IZIGoHTTPRequest) ToFile(filename string) error

ToFile saves the body data in response to one file. it calls Response inner.

func (*IZIGoHTTPRequest) ToJSON

func (b *IZIGoHTTPRequest) ToJSON(v interface{}) error

ToJSON returns the map that marshals from the body bytes as json in response . it calls Response inner.

func (*IZIGoHTTPRequest) ToXML

func (b *IZIGoHTTPRequest) ToXML(v interface{}) error

ToXML returns the map that marshals from the body bytes as xml in response . it calls Response inner.

func (*IZIGoHTTPRequest) ToYAML

func (b *IZIGoHTTPRequest) ToYAML(v interface{}) error

ToYAML returns the map that marshals from the body bytes as yaml in response . it calls Response inner.

func (*IZIGoHTTPRequest) XMLBody

func (b *IZIGoHTTPRequest) XMLBody(obj interface{}) (*IZIGoHTTPRequest, error)

XMLBody adds request raw body encoding by XML.

func (*IZIGoHTTPRequest) YAMLBody

func (b *IZIGoHTTPRequest) YAMLBody(obj interface{}) (*IZIGoHTTPRequest, error)

YAMLBody adds request raw body encoding by YAML.

type IZIGoHTTPSettings

type IZIGoHTTPSettings struct {
	ShowDebug        bool
	UserAgent        string
	ConnectTimeout   time.Duration
	ReadWriteTimeout time.Duration
	TLSClientConfig  *tls.Config
	Proxy            func(*http.Request) (*url.URL, error)
	Transport        http.RoundTripper
	CheckRedirect    func(req *http.Request, via []*http.Request) error
	EnableCookie     bool
	Gzip             bool
	DumpBody         bool
	Retries          int // if set to -1 means will retry forever
}

IZIGoHTTPSettings is the http.Client setting

Source Files

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
t or T : Toggle theme light dark auto
y or Y : Canonical URL