Skip to content

icyxp/go-httplib

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 

Repository files navigation

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/icyxp/go-httplib"

str, err := httplib.Get("https://jiunile.com/").String()
if err != nil {
    	// error
}
fmt.Println(str)

POST

POST data to remote url

req := httplib.Post("https://jiunile.com/")
req.Param("username","jiunile")
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("https://jiunile.com/").SetTimeout(100 * time.Second, 30 * time.Second)

// POST
httplib.Post("https://jiunile.com/").SetTimeout(100 * time.Second, 30 * time.Second)

Debug

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

httplib.Get("https://jiunile.com/").Debug(true)

Set HTTP Basic Auth

str, err := Get("https://jiunile.com/").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 https://golang.org/pkg/crypto/tls/#Config

Set HTTP Version

some servers need to specify the protocol version of HTTP

httplib.Get("https://jiunile.com/").SetProtocolVersion("HTTP/1.1")

Set Cookie

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

cookie := &http.Cookie{}
cookie.Name = "username"
cookie.Value  = "jiunile"
httplib.Get("https://jiunile.com/").SetCookie(cookie)

Upload file

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

req := httplib.Post("https://jiunile.com/")
req.Param("username","jiunile")
req.PostFile("uploadfile1", "httplib.pdf")
str, err := req.String()
if err != nil {
    	// error
}
fmt.Println(str)

See godoc for further documentation and examples.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages