ok

package module
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Aug 25, 2016 License: MIT Imports: 6 Imported by: 0

README

ok

The most simple but handy HTTP request library for Go.

Installation

$ go get github.com/syaning/ok

Getting Started

The exported struct ok.RequestWrapper wraps a client, an HTTP request, an HTTP response and a potential error together. When sending a request and get respones, there are 4 steps:

  1. Create a RequestWrapper
    • Request()
    • NewRequest(method, urlStr string)
    • Get(urlStr string)
    • Post(urlStr string)
    • Put(urlStr string)
    • Delete(urlStr string)
    • ...
  2. Prepare the request: set headers, set body, use proxy, ...
    • Method(method string)
    • Url(urlStr string)
    • Query(query string)
    • Set(key, value string)
    • Proxy(proxy string)
    • ...
  3. Send request
    • OK()
  4. Get response and read response body
    • ToBytes()
    • ToString()
    • Pipe(w io.Writer)
    • ToFile(filename string)
    • ...

See full functions and methods on GoDoc.

Examples

HTTP Get
str, err := ok.
    Get("http://httpbin.org/get").
    OK().
    ToString()
fmt.Println(str, err)
HTTP Post JSON
size, err := ok.
    Post("http://httpbin.org/post").
    Json(`{"greeting":"hello world"}`).
    OK().
    Pipe(os.Stdout)
fmt.Println(size, err)
HTTP Post Form
size, err := ok.
    Post("http://httpbin.org/post").
    Json("greeting=hello world").
    OK().
    ToFile("res.json")
fmt.Println(size, err)
Download
size, err := ok.Download("http://httpbin.org/image/png", "img.png")
fmt.Println(size, err)

License

MIT

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Download

func Download(urlStr, filename string) (size int64, err error)

Types

type RequestWrapper added in v0.1.1

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

func Delete

func Delete(urlStr string) *RequestWrapper

func Get

func Get(urlStr string) *RequestWrapper

func NewRequest

func NewRequest(method, urlStr string) *RequestWrapper

func Post

func Post(urlStr string) *RequestWrapper

func Put

func Put(urlStr string) *RequestWrapper

func Request

func Request() *RequestWrapper

func (*RequestWrapper) BasicAuth added in v0.1.1

func (r *RequestWrapper) BasicAuth(username, password string) *RequestWrapper

func (*RequestWrapper) Client added in v0.1.1

func (r *RequestWrapper) Client() *http.Client

func (*RequestWrapper) Form added in v0.1.1

func (r *RequestWrapper) Form(data string) *RequestWrapper

func (*RequestWrapper) Header added in v0.1.1

func (r *RequestWrapper) Header(key, value string) *RequestWrapper

func (*RequestWrapper) JSON added in v0.1.1

func (r *RequestWrapper) JSON(data string) *RequestWrapper

func (*RequestWrapper) Json added in v0.1.1

func (r *RequestWrapper) Json(data string) *RequestWrapper

func (*RequestWrapper) Method added in v0.1.1

func (r *RequestWrapper) Method(method string) *RequestWrapper

func (*RequestWrapper) OK added in v0.1.1

func (r *RequestWrapper) OK() *RequestWrapper

func (*RequestWrapper) Param added in v0.1.1

func (r *RequestWrapper) Param(key, value string) *RequestWrapper

func (*RequestWrapper) Pipe added in v0.1.1

func (r *RequestWrapper) Pipe(w io.Writer) (written int64, err error)

func (*RequestWrapper) Proxy added in v0.1.1

func (r *RequestWrapper) Proxy(proxy string) *RequestWrapper

func (*RequestWrapper) ProxyFn added in v0.1.1

func (r *RequestWrapper) ProxyFn(proxyFn func(*http.Request) (*url.URL, error)) *RequestWrapper

func (*RequestWrapper) Query added in v0.1.1

func (r *RequestWrapper) Query(query string) *RequestWrapper

func (*RequestWrapper) Request added in v0.1.1

func (r *RequestWrapper) Request() *http.Request

func (*RequestWrapper) Response added in v0.1.1

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

func (*RequestWrapper) Set added in v0.1.1

func (r *RequestWrapper) Set(key, value string) *RequestWrapper

func (*RequestWrapper) ToBytes added in v0.1.1

func (r *RequestWrapper) ToBytes() ([]byte, error)

func (*RequestWrapper) ToFile added in v0.1.1

func (r *RequestWrapper) ToFile(filename string) (size int64, err error)

func (*RequestWrapper) ToString added in v0.1.1

func (r *RequestWrapper) ToString() (string, error)

func (*RequestWrapper) Url added in v0.1.1

func (r *RequestWrapper) Url(urlStr string) *RequestWrapper

func (*RequestWrapper) Use added in v0.1.1

func (r *RequestWrapper) Use(client *http.Client) *RequestWrapper

Jump to

Keyboard shortcuts

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