Categorygithub.com/telanflow/quick
modulepackage
0.4.1
Repository: https://github.com/telanflow/quick.git
Documentation: pkg.go.dev

# README

Quick

GoDoc

Simple and efficient HTTP request library

简单高效的Http请求库

examples

package main

import (
    "fmt"
    "github.com/telanflow/quick"
    "net/http"
)

func main() {
    // quick.Post("example.com")
    // quick.PostFormData("example.com")
    // quick.Put("example.com")
    // quick.Head("example.com")
    // quick.Delete("example.com")
    // quick.Patch("example.com")
    // quick.Options("example.com")
    // quick.Trace("example.com")

    // https ssl skip verify 取消https验证
    quick.InsecureSkipVerify(true)

    // set header
    quick.SetHeader(http.Header{
        "Context-Type": []string{"text/html"},
    })
    // set UserAgent to request
    quick.SetHeaderSingle("User-Agent", "A go request libraries for quick")
    quick.SetUserAgent("A go request libraries for quick")
    
    // You should init it by using NewCookiesWithString like this:
    // 	cookies := quick.NewCookiesWithString(
    //		"key1=value1; key2=value2; key3=value3"
    // 	)
    // Note: param is cookie string
    cookies := quick.NewCookiesWithString("sessionid=11111")
    
    // request
    resp, err := quick.Get(
        "http://example.com?bb=1", 
        quick.OptionQueryString("name=quick&aa=11"),   // set Get params   eg. "example.com?bb=1&name=quick&aa=11"
        quick.OptionHeaderSingle("User-Agent", ""),    // set http header
        quick.OptionHeader(http.Header{}),             // set http header  eg. http.Header || map[string]string || []string
        quick.OptionRedirectNum(10),                   // set redirect num
        quick.OptionCookies(cookies),                  // set cookies to request
        // quick.OptionProxy("http://127.0.0.1:8080"), // set proxy address
        // quick.OptionBody(""),                       // POST body
        // quick.OptionBasicAuth("username", "password"), // HTTP Basic Authentication
        // ... quick.Option
    )
    if err != nil {
        panic(err)
    }

    fmt.Println(resp)
}

Session (会话)

Request based session

所有Request都基于session(http.Client)

func main() {
    // cookieJar
    cookieJar, err := quick.NewCookieJar()
    if err != nil {
        panic(err)
    }
    
    // quick use default global session
    // create session
    session := quick.NewSession()
    // https ssl skip verify 取消https验证
    session.InsecureSkipVerify(true)
    // set cookieJar
    session.SetCookieJar(cookieJar) 
    resp, err := session.Get("http://example.com")
    if err != nil {
        panic(err)
    }
    //resp.Status       e.g. "200 OK"
    //resp.StatusCode   e.g. 200
    //... 
    fmt.Println(resp)
}

Other example:

func main() {
    // new Request
    req := quick.NewRequest().SetUrl("http://example.com").SetMethod(http.MethodGet)

    // send Request
    session := quick.NewSession()
	session.EnableTrace() // trace
    resp, err := session.Suck(
        req, 
        quick.OptionHeaderSingle("User-Agent", ""), // set http header
        // ... quick.Option
    )
    if err != nil {
        panic(err)
    }

    // resp.Status       e.g. "200 OK"
    // resp.StatusCode   e.g. 200
    // ...
    // 
    // TraceInfo:
    //      DNSLookup: 4ms
    //      ConnTime: 1230ms
    //      TCPConnTime: 405ms
    //      TLSHandshake: 819ms
    //      ServerTime: 299ms
    fmt.Println(resp)
    fmt.Println(resp.TraceInfo())
}

Middleware(中间件)

func main() {
    // new Request
    req := quick.NewRequest().SetUrl("http://example.com").SetMethod(http.MethodGet)

    // create session
    session := quick.NewSession()

    // use middleware
    session.Use(
        // middleware 1
        func(r *http.Request) {
            log.Printf(
                "Middleware: %v RedirectNum: %v Proxy: %v \n",
                r.URL,
                r.Context().Value(quick.ContextRedirectNumKey),
                r.Context().Value(quick.ContextProxyKey),
            )
        },
        // middleware 2
        func(r *http.Request) {
            log.Printf(
                "Middleware2: %v RedirectNum: %v Proxy: %v \n",
                r.URL,
                r.Context().Value(quick.ContextRedirectNumKey),
                r.Context().Value(quick.ContextProxyKey),
            )
        },
    )

    // send Request
    resp, err := session.Suck(
        req, 
        quick.OptionHeaderSingle("User-Agent", ""), // set http header
        // ... quick.Option
    )
    if err != nil {
        panic(err)
    }

    //resp.Status       e.g. "200 OK"
    //resp.StatusCode   e.g. 200
    //... 
    fmt.Println(resp)
}

License

MIT

# Packages

No description provided by the author

# Functions

No description provided by the author
Connect request.
ConvertHttpRequest convert http.Request To Request.
CopyHeader copy headers.
CopyURL copy a new url.URL.
DefaultSessionOptions return a default SessionOptions object.
Delete request.
DisableTrace method disables the Quick client trace.
Do send http.Request.
Download download file to save hard disk.
EnableTrace method enables the Quick client trace for the requests fired from the client using `httptrace.ClientTrace` and provides insights.
Get request.
GetHeader get global header.
GetHeaderSingle get global header single.
GetProxyUrl get session global proxy url.
GetProxyURL get session global proxy url.
GetUserAgent get global user-agent.
Head request.
InsecureSkipVerify ssl skip verify.
MergeHeaders merge Request headers and Session Headers.
MergeQueryString Get request merge url and query string encode.
create a cookieJar.
You should init it by using NewCookiesWithString like this: cookies := quick.NewCookiesWithString( "key1=value1; key2=value2; key3=value3" ) Note: param is cookie string.
NewRequest create a request instance.
NewRequestWithContext create a request instance with context.Context.
NewSession create a session.
OptionBasicAuth HTTP Basic Authentication.
OptionBody request body for post.
OptionBodyFormData request body for post (FormData).
OptionBodyJSON request body for post.
OptionBodyXML request body for post.
OptionBodyXWwwFormUrlencoded set request body x-www-form-urlencoded.
OptionCookies set cookies to request.
OptionHeader set request header.
OptionHeaderSingle set an http header to request.
OptionProxy set proxy for request.
OptionQueryString request query string for get.
OptionRedirectNum set redirect num to request.
Options request.
OptionTimeout set timeout to request.
Patch request.
Post
Post request.
PostFormData request.
Put request.
ReplaceQueryString Get request replace url and query string encode.
SetBaseURL method is to set Base URL in the client instance.
SetCheckRedirectHandler set global checkRedirect handler handler: func(req *http.Request, via []*http.Request) error.
SetCookieJar set global cookieJar.
SetHeader set global header.
SetHeaderSingle set global header single.
SetLogger method sets given writer for logging Quick request and response details.
SetProxyHandler set global proxy handler handler: func(req *http.Request) (*url.URL, error).
SetProxyUrl set global proxy url.
SetProxyURL set global proxy url.
SetTimeout set global request timeout example: time.Second * 30.
SetUserAgent set global user-agent.
Suck request suck data.
Use use middleware handler.
WrapErr will wrap a error with some information: filename, line, time and some message.
WrapErr will wrap a error with some information: filename, line, time and some message.

# Constants

request context proxy key name.
request context redirect num key name.
default redirect num.

# Variables

No description provided by the author
No description provided by the author

# Structs

No description provided by the author
No description provided by the author
Request http request payload.
No description provided by the author
Session is a http.Client.
No description provided by the author
TraceInfo struct is used provide request trace info such as DNS lookup duration, Connection obtain duration, Server processing duration, etc.

# Interfaces

Logger interface is to abstract the logging from quick.

# Type aliases

defined []http.Cookie alias Cookies.
No description provided by the author
OptionFunc request option func.