httplib

package
v0.0.0-...-d032931 Latest Latest
Warning

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

Go to latest
Published: Jun 1, 2018 License: GPL-3.0 Imports: 19 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/CloudWise-OpenSource/GoCrab/Libs/httplib"

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

POST

POST data to remote url

req := httplib.Post("http://GoCrab.me/")
req.Param("username","Neeke")
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)

Exmaple:

// GET
httplib.Get("http://GoCrab.me/").SetTimeout(100 * time.Second, 30 * time.Second)

// POST
httplib.Post("http://GoCrab.me/").SetTimeout(100 * time.Second, 30 * time.Second)

Debug

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

httplib.Get("http://GoCrab.me/").Debug(true)

Set HTTP Basic Auth

str, err := Get("http://GoCrab.me/").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://GoCrab.me/").SetProtocolVersion("HTTP/1.1")

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

cookie := &http.Cookie{}
cookie.Name = "username"
cookie.Value  = "Neeke"
httplib.Get("http://GoCrab.me/").SetCookie(cookie)

Upload file

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

req := httplib.Post("http://GoCrab.me/")
req.Param("username","Neeke")
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

Usage:

import "github.com/CloudWise-OpenSource/GoCrab/Libs/httplib"

b := httplib.Post("http://GoCrab.me/")
b.Param("username","Neeke")
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)

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func SetDefaultSetting

func SetDefaultSetting(setting GoCrabHttpSettings)

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 GoCrabHttpRequest

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

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

func Delete

func Delete(url string) *GoCrabHttpRequest

Delete returns *GoCrabHttpRequest DELETE method.

func Get

func Get(url string) *GoCrabHttpRequest

Get returns *GoCrabHttpRequest with GET method.

func Head(url string) *GoCrabHttpRequest

Head returns *GoCrabHttpRequest with HEAD method.

func Post

func Post(url string) *GoCrabHttpRequest

Post returns *GoCrabHttpRequest with POST method.

func Put

func Put(url string) *GoCrabHttpRequest

Put returns *GoCrabHttpRequest with PUT method.

func (*GoCrabHttpRequest) Body

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

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

func (*GoCrabHttpRequest) Bytes

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

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

func (*GoCrabHttpRequest) Debug

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

Debug sets show debug or not when executing request.

func (*GoCrabHttpRequest) Header

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

Header add header item string in request.

func (*GoCrabHttpRequest) Param

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

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

func (*GoCrabHttpRequest) PostFile

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

func (*GoCrabHttpRequest) Response

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

Response executes request client gets response mannually.

func (*GoCrabHttpRequest) SetBasicAuth

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

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

func (*GoCrabHttpRequest) SetCompress

func (b *GoCrabHttpRequest) SetCompress(compress string) *GoCrabHttpRequest

func (*GoCrabHttpRequest) SetCookie

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

SetCookie add cookie into request.

func (*GoCrabHttpRequest) SetEnableCookie

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

SetEnableCookie sets enable/disable cookiejar

func (*GoCrabHttpRequest) SetProtocolVersion

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

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

func (*GoCrabHttpRequest) SetProxy

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

Set http proxy example:

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

func (*GoCrabHttpRequest) SetTLSClientConfig

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

SetTLSClientConfig sets tls connection configurations if visiting https url.

func (*GoCrabHttpRequest) SetTimeout

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

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

func (*GoCrabHttpRequest) SetTransport

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

Set transport to

func (*GoCrabHttpRequest) SetUserAgent

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

SetUserAgent sets User-Agent header field

func (*GoCrabHttpRequest) Setting

Change request settings

func (*GoCrabHttpRequest) Status

func (b *GoCrabHttpRequest) Status() (int, error)

只获取响应status

func (*GoCrabHttpRequest) String

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

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

func (*GoCrabHttpRequest) ToFile

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

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

func (*GoCrabHttpRequest) ToJson

func (b *GoCrabHttpRequest) ToJson(v interface{}) error

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

func (*GoCrabHttpRequest) ToXml

func (b *GoCrabHttpRequest) ToXml(v interface{}) error

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

type GoCrabHttpSettings

type GoCrabHttpSettings struct {
	Compress         string
	ShowDebug        bool
	UserAgent        string
	ConnectTimeout   time.Duration
	ReadWriteTimeout time.Duration
	TlsClientConfig  *tls.Config
	Proxy            func(*http.Request) (*url.URL, error)
	Transport        http.RoundTripper
	EnableCookie     bool
}

GoCrabHttpSettings

Jump to

Keyboard shortcuts

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