reqwest

package module
v0.0.0-...-cdaa769 Latest Latest
Warning

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

Go to latest
Published: Sep 29, 2019 License: MIT Imports: 22 Imported by: 0

README

reqwest

A simple and user-friendly HTTP request library for Go.

Build Status Go Report Card GoDoc License

Features

  • GET, HEAD, POST, PUT, PATCH, DELETE, OPTIONS, etc.
  • Easy set query params, headers and cookies.
  • Easy send form, JSON or files payload.
  • Easy set basic authentication or bearer token.
  • Easy customize root certificate authorities and client certificates.
  • Easy set proxy.
  • Automatic cookies management.
  • Customize HTTP client, transport, redirect policy, cookie jar and timeout.
  • Easy set context.
  • Easy decode responses, raw data, text representation and unmarshal the JSON-encoded data.
  • Concurrent safe.

Install

go get -u github.com/winterssy/reqwest

Usage

import "github.com/winterssy/reqwest"

Examples

Set Params
data, err := reqwest.
    Get("http://httpbin.org/get").
    Params(reqwest.Value{
        "key1": "value1",
        "key2": "value2",
    }).
    Send().
    Text()
if err != nil {
    panic(err)
}
fmt.Println(data)
Set Headers
data, err := reqwest.
    Get("http://httpbin.org/get").
    Headers(reqwest.Value{
        "Origin":  "http://httpbin.org",
        "Referer": "http://httpbin.org",
    }).
    Send().
    Text()
if err != nil {
    panic(err)
}
fmt.Println(data)
Set Cookies
data, err := reqwest.
    Get("http://httpbin.org/cookies/set").
    Cookies(
        &http.Cookie{
            Name:  "name1",
            Value: "value1",
        },
        &http.Cookie{
            Name:  "name2",
            Value: "value2",
        },
    ).
    Send().
    Text()
if err != nil {
    panic(err)
}
fmt.Println(data)
Set Form Payload
data, err := reqwest.
    Post("http://httpbin.org/post").
    Form(reqwest.Value{
        "key1": "value1",
        "key2": "value2",
    }).
    Send().
    Text()
if err != nil {
    panic(err)
}
fmt.Println(data)
Set JSON Payload
data, err := reqwest.
    Post("http://httpbin.org/post").
    JSON(reqwest.Data{
        "msg": "hello world",
        "num": 2019,
    }).
    Send().
    Text()
if err != nil {
    panic(err)
}
fmt.Println(data)
Set Files Payload
data, err := reqwest.
    Post("http://httpbin.org/post").
    Files(
        &reqwest.File{
            FieldName: "testimage1",
            FileName:  "testimage1.jpg",
            FilePath:  "./testdata/testimage1.jpg",
        },
        &reqwest.File{
            FieldName: "testimage2",
            FileName:  "testimage2.jpg",
            FilePath:  "./testdata/testimage2.jpg",
        },
    ).
    Send().
    Text()
if err != nil {
    panic(err)
}
fmt.Println(data)
Set Basic Authentication
data, err := reqwest.
    Get("http://httpbin.org/basic-auth/admin/pass").
    BasicAuth("admin", "pass").
    Send().
    Text()
if err != nil {
    panic(err)
}
fmt.Println(data)
Set Bearer Token
data, err := reqwest.
    Get("http://httpbin.org/bearer").
    BearerToken("reqwest").
    Send().
    Text()
if err != nil {
    panic(err)
}
fmt.Println(data)
Customize HTTP Client
transport := &http.Transport{
    Proxy: http.ProxyFromEnvironment,
    DialContext: (&net.Dialer{
        Timeout:   30 * time.Second,
        KeepAlive: 30 * time.Second,
    }).DialContext,
    MaxIdleConns:          100,
    IdleConnTimeout:       90 * time.Second,
    TLSHandshakeTimeout:   10 * time.Second,
    ExpectContinueTimeout: 1 * time.Second,
}
redirectPolicy := func(req *http.Request, via []*http.Request) error {
    return http.ErrUseLastResponse
}
jar, _ := cookiejar.New(&cookiejar.Options{
    PublicSuffixList: publicsuffix.List,
})
timeout := 120 * time.Second

httpClient := &http.Client{
    Transport:     transport,
    CheckRedirect: redirectPolicy,
    Jar:           jar,
    Timeout:       timeout,
}
data, err := reqwest.
    WithHTTPClient(httpClient).
    Get("http://httpbin.org/get").
    Send().
    Text()
if err != nil {
    panic(err)
}
fmt.Println(data)
Set Proxy
data, err := reqwest.
    WithProxy("http://127.0.0.1:1081").
    Get("http://httpbin.org/get").
    Send().
    Text()
if err != nil {
    panic(err)
}
fmt.Println(data)
Concurrent Safe

Use reqwest across goroutines you must call AcquireLock for each request in the beginning, otherwise might cause data race. Don't forget it!

const MaxWorker = 1000
wg := new(sync.WaitGroup)

for i := 0; i < MaxWorker; i += 1 {
    wg.Add(1)
    go func(i int) {
        defer wg.Done()

        params := reqwest.Value{}
        params.Set(fmt.Sprintf("key%d", i), fmt.Sprintf("value%d", i))

        data, err := reqwest.
            AcquireLock().
            Get("http://httpbin.org/get").
            Params(params).
            Send().
            Text()
        if err != nil {
            return
        }

        fmt.Println(data)
    }(i)
}

wg.Wait()

License

MIT.

Thanks

Documentation

Index

Constants

View Source
const (
	// Version of reqwest.
	Version = "0.1"

	// ContentType is the same as "Content-Type".
	ContentType = "Content-Type"

	// TypeForm is the same as "application/x-www-form-urlencoded".
	TypeForm = "application/x-www-form-urlencoded"

	// TypeJSON is the same as "application/json".
	TypeJSON = "application/json"

	// MethodGet represents GET HTTP method
	MethodGet = "GET"

	// MethodHead represents HEAD HTTP method
	MethodHead = "HEAD"

	// MethodPost represents POST HTTP method
	MethodPost = "POST"

	// MethodPut represents PUT HTTP method
	MethodPut = "PUT"

	// MethodPatch represents PATCH HTTP method
	MethodPatch = "PATCH"

	// MethodDelete represents DELETE HTTP method
	MethodDelete = "DELETE"

	// MethodConnect represents CONNECT HTTP method
	MethodConnect = "CONNECT"

	// MethodOptions represents OPTIONS HTTP method
	MethodOptions = "OPTIONS"

	// MethodTrace represents TRACE HTTP method
	MethodTrace = "TRACE"
)

Variables

This section is empty.

Functions

func Reset

func Reset()

Reset resets state of the default reqwest client.

Types

type Client

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

Client defines a reqwest client.

func AcquireLock

func AcquireLock() *Client

AcquireLock locks the default reqwest client. Use reqwest across goroutines you must call AcquireLock for each request in the beginning, otherwise might cause data race. Don't forget it!

func BasicAuth

func BasicAuth(username, password string) *Client

BasicAuth sets basic authentication of the default reqwest client.

func BearerToken

func BearerToken(token string) *Client

BearerToken sets bearer token of the default reqwest client.

func Connect

func Connect(url string) *Client

Connect uses the default reqwest client to make CONNECT HTTP requests.

func Cookies

func Cookies(cookies ...*http.Cookie) *Client

Cookies sets cookies of the default reqwest client.

func Delete

func Delete(url string) *Client

Delete uses the default reqwest client to make DELETE HTTP requests.

func DisableKeepAlives

func DisableKeepAlives() *Client

DisableKeepAlives disables HTTP keep-alives of the default reqwest client. HTTP keep-alives is enabled by default.

func DisableProxy

func DisableProxy() *Client

DisableProxy lets the default reqwest client not use proxy. reqwest uses proxy from environment by default.

func DisableRedirect

func DisableRedirect() *Client

DisableRedirect lets the default reqwest client not redirect HTTP requests. HTTP requests redirection is enabled by default.

func DisableSession

func DisableSession() *Client

DisableSession lets the default reqwest client not use cookie jar. Session is enabled by default, reqwest use cookie jar to manage cookies automatically.

func DisableVerify

func DisableVerify() *Client

DisableVerify lets the default reqwest client not verify the server's TLS certificate. TLS certificate verification is enabled by default.

func Files

func Files(files ...*File) *Client

Files sets files payload of the default reqwest client.

func Form

func Form(form Value) *Client

Form sets form payload of the default reqwest client.

func Get

func Get(url string) *Client

Get uses the default reqwest client to make GET HTTP requests.

func Head(url string) *Client

Head uses the default reqwest client to make HEAD HTTP requests.

func Headers

func Headers(headers Value) *Client

Headers sets headers of the default reqwest client.

func Host

func Host(host string) *Client

Host specifies the host of the default reqwest client on which the URL is sought.

func JSON

func JSON(data Data) *Client

JSON sets JSON payload of the default reqwest client.

func New

func New() *Client

New constructors and returns a new reqwest client.

func Options

func Options(url string) *Client

Options uses the default reqwest client to make OPTIONS HTTP requests.

func Params

func Params(params Value) *Client

Params sets query params of the default reqwest client.

func Patch

func Patch(url string) *Client

Patch uses the default reqwest client to make PATCH HTTP requests.

func Post

func Post(url string) *Client

Post uses the default reqwest client to make POST HTTP requests.

func Put

func Put(url string) *Client

Put uses the default reqwest client to make PUT HTTP requests.

func Trace

func Trace(url string) *Client

Trace uses the default reqwest client to make TRACE HTTP requests.

func WithClientCertificates

func WithClientCertificates(certs ...tls.Certificate) *Client

WithClientCertificates appends client certificates of the default reqwest client.

func WithContext

func WithContext(ctx context.Context) *Client

WithContext sets HTTP requests context of the default reqwest client.

func WithCookieJar

func WithCookieJar(jar http.CookieJar) *Client

WithCookieJar changes cookie jar of the default reqwest client.

func WithHTTPClient

func WithHTTPClient(httpClient *http.Client) *Client

WithHTTPClient changes HTTP client of the default reqwest client.

func WithProxy

func WithProxy(url string) *Client

WithProxy sets proxy of the default reqwest client from a url.

func WithRedirectPolicy

func WithRedirectPolicy(policy func(req *http.Request, via []*http.Request) error) *Client

WithRedirectPolicy changes redirection policy of the default reqwest client.

func WithRootCA

func WithRootCA(pemFilePath string) *Client

WithRootCA appends root certificate authorities of the default reqwest client.

func WithTimeout

func WithTimeout(timeout time.Duration) *Client

WithTimeout changes timeout of the default reqwest client, zero means no timeout.

func WithTransport

func WithTransport(transport http.RoundTripper) *Client

WithTransport changes transport of the default reqwest client.

func (*Client) AcquireLock

func (c *Client) AcquireLock() *Client

AcquireLock locks c. Use reqwest across goroutines you must call AcquireLock for each request in the beginning, otherwise might cause data race. Don't forget it!

func (*Client) BasicAuth

func (c *Client) BasicAuth(username, password string) *Client

BasicAuth sets basic authentication of c.

func (*Client) BearerToken

func (c *Client) BearerToken(token string) *Client

BearerToken sets bearer token of c.

func (*Client) Connect

func (c *Client) Connect(url string) *Client

Connect uses c to make CONNECT HTTP requests.

func (*Client) Cookies

func (c *Client) Cookies(cookies ...*http.Cookie) *Client

Cookies sets cookies of c.

func (*Client) Delete

func (c *Client) Delete(url string) *Client

Delete uses c to make DELETE HTTP requests.

func (*Client) DisableKeepAlives

func (c *Client) DisableKeepAlives() *Client

DisableKeepAlives disables HTTP keep-alives of c. HTTP keep-alives is enabled by default.

func (*Client) DisableProxy

func (c *Client) DisableProxy() *Client

DisableProxy lets c not use proxy. reqwest uses proxy from environment by default.

func (*Client) DisableRedirect

func (c *Client) DisableRedirect() *Client

DisableRedirect lets c not redirect HTTP requests. HTTP requests redirection is enabled by default.

func (*Client) DisableSession

func (c *Client) DisableSession() *Client

DisableSession lets c not use cookie jar. Session is enabled by default, reqwest use cookie jar to manage cookies automatically.

func (*Client) DisableVerify

func (c *Client) DisableVerify() *Client

DisableVerify lets c not verify the server's TLS certificate. TLS certificate verification is enabled by default.

func (*Client) Files

func (c *Client) Files(files ...*File) *Client

Files sets files payload of c.

func (*Client) Form

func (c *Client) Form(form Value) *Client

Form sets form payload of c.

func (*Client) Get

func (c *Client) Get(url string) *Client

Get uses c to make GET HTTP requests.

func (*Client) Head

func (c *Client) Head(url string) *Client

Head uses c to make HEAD HTTP requests.

func (*Client) Headers

func (c *Client) Headers(headers Value) *Client

Headers sets headers of c.

func (*Client) Host

func (c *Client) Host(host string) *Client

Host specifies the host of c on which the URL is sought.

func (*Client) JSON

func (c *Client) JSON(data Data) *Client

JSON sets JSON payload of c.

func (*Client) Options

func (c *Client) Options(url string) *Client

Options uses c to make OPTIONS HTTP requests.

func (*Client) Params

func (c *Client) Params(params Value) *Client

Params sets query params of c.

func (*Client) Patch

func (c *Client) Patch(url string) *Client

Patch uses c to make PATCH HTTP requests.

func (*Client) Post

func (c *Client) Post(url string) *Client

Post uses c to make POST HTTP requests.

func (*Client) Put

func (c *Client) Put(url string) *Client

Put uses c to make PUT HTTP requests.

func (*Client) Reset

func (c *Client) Reset()

Reset resets state of c so that other requests can acquire lock.

func (*Client) Send

func (c *Client) Send() *Response

Send uses c to send the HTTP request and returns its response.

func (*Client) Trace

func (c *Client) Trace(url string) *Client

Trace uses c to make TRACE HTTP requests.

func (*Client) WithClientCertificates

func (c *Client) WithClientCertificates(certs ...tls.Certificate) *Client

WithClientCertificates appends client certificates of c.

func (*Client) WithContext

func (c *Client) WithContext(ctx context.Context) *Client

WithContext sets HTTP requests context of c.

func (*Client) WithCookieJar

func (c *Client) WithCookieJar(jar http.CookieJar) *Client

WithCookieJar sets cookie jar of c.

func (*Client) WithHTTPClient

func (c *Client) WithHTTPClient(httpClient *http.Client) *Client

WithHTTPClient sets HTTP client of c.

func (*Client) WithProxy

func (c *Client) WithProxy(url string) *Client

WithProxy sets proxy of c from a url.

func (*Client) WithRedirectPolicy

func (c *Client) WithRedirectPolicy(policy func(req *http.Request, via []*http.Request) error) *Client

WithRedirectPolicy sets redirection policy of c.

func (*Client) WithRootCA

func (c *Client) WithRootCA(pemFilePath string) *Client

WithRootCA appends root certificate authorities of c.

func (*Client) WithTimeout

func (c *Client) WithTimeout(timeout time.Duration) *Client

WithTimeout sets timeout of c, zero means no timeout.

func (*Client) WithTransport

func (c *Client) WithTransport(transport http.RoundTripper) *Client

WithTransport sets transport of c.

type Data

type Data map[string]interface{}

Data is the same as map[string]interface{}, used for JSON payload.

func (Data) Del

func (d Data) Del(key string)

Del deletes the value related to the given key from a map.

func (Data) Get

func (d Data) Get(key string) interface{}

Get returns the value from a map by the given key.

func (Data) Set

func (d Data) Set(key string, value interface{})

Set sets a kv pair into a map.

type File

type File struct {
	// FieldName specifies the field name of the file you want to upload.
	FieldName string `json:"fieldname,omitempty"`

	// FileName specifies the file name of the file you want to upload.
	FileName string `json:"filename,omitempty"`

	// FilePath specifies the file path of the file you want to upload.
	FilePath string `json:"-"`
}

File defines a multipart-data.

func (*File) String

func (f *File) String() string

String returns the JSON-encoded text representation of a file.

type Response

type Response struct {
	// R specifies the original HTTP response.
	R *http.Response

	// Err specifies the potential error.
	Err error
}

Response wraps the original HTTP response and the potential error.

func Send

func Send() *Response

Send uses the default reqwest client to send the HTTP request and returns its response.

func (*Response) EnsureStatus2xx

func (r *Response) EnsureStatus2xx() *Response

EnsureStatus2xx ensures the HTTP response's status code of r must be 2xx.

func (*Response) EnsureStatusOk

func (r *Response) EnsureStatusOk() *Response

EnsureStatusOk ensures the HTTP response's status code of r must be 200.

func (*Response) JSON

func (r *Response) JSON(v interface{}) error

JSON decodes the HTTP response body of r and unmarshals its JSON-encoded data into v.

func (*Response) Raw

func (r *Response) Raw() ([]byte, error)

Raw decodes the HTTP response body of r and returns its raw data.

func (*Response) Resolve

func (r *Response) Resolve() (*http.Response, error)

Resolve resolves r and returns its original HTTP response.

func (*Response) Text

func (r *Response) Text() (string, error)

Text decodes the HTTP response body of r and returns the text representation of its raw data.

type Value

type Value map[string]string

Value is the same as map[string]string, used for params, headers, form, etc.

func (Value) Del

func (v Value) Del(key string)

Del deletes the value related to the given key from a map.

func (Value) Get

func (v Value) Get(key string) string

Get returns the value from a map by the given key.

func (Value) Set

func (v Value) Set(key string, value string)

Set sets a kv pair into a map.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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