request

package module
v1.4.2 Latest Latest
Warning

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

Go to latest
Published: Feb 23, 2017 License: MIT Imports: 9 Imported by: 4

README

request Build Status Doc

Simplified HTTP request client in go

installation

go get gopkg.in/ddo/request.v1

option

  • Url string required
  • Method string default: "GET", anything "POST", "PUT", "DELETE" or "PATCH"
  • BodyStr string
  • Body *Data
  • Form *Data set Content-Type header as "application/x-www-form-urlencoded"
  • Json interface{} set Content-Type header as "application/json"
  • Query *Data
  • Header *Header
GET
client := request.New()

res, err := client.Request(&request.Option{
    Url: "https://httpbin.org/get",
})

if err != nil {
    panic(err)
}

defer res.Body.Close()
POST
res, err := client.Request(&request.Option{
    Url:    "https://httpbin.org/post",
    Method: "POST",
    Body: &request.Data{
        "two":   []string{"2", "hai"},
        "three": []string{"3", "ba", "trois"},
        "email": []string{"ddo@ddo.me"},
    },
})
POST form
res, err := client.Request(&request.Option{
    Url:    "https://httpbin.org/post",
    Method: "POST",
    Form: &request.Data{
        "two":   []string{"2", "hai"},
        "three": []string{"3", "ba", "trois"},
        "email": []string{"ddo@ddo.me"},
    },
})
Json
res, err := client.Request(&request.Option{
    Url:    "https://httpbin.org/post",
    Method: "POST",
    Json: map[string]interface{}{
        "int":    1,
        "string": "two",
        "array":  []string{"3", "ba", "trois"},
        "object": map[string]interface{}{
            "int": 4,
        },
    },
})

logger

to enable log set environment variable as

DLOG=*

or

DEBUG=* go run file.go

test

go test -v

TODO

  • default settings
  • hooks
  • file

Documentation

Index

Constants

View Source
const (
	// DefaultTimeout is request timeout in second
	DefaultTimeout = 180
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

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

Client is an http client that hold init settings and cookies

func New

func New() *Client

New return a new Client

func NewNoCookie added in v1.1.0

func NewNoCookie() *Client

NewNoCookie return a new Client that won't save cookies

func (*Client) ExportCookie added in v1.4.0

func (c *Client) ExportCookie(domain string) (jsonStr string, err error)

ExportCookie exports client cookies as json stdlib cookie jar just export cookie name and value

func (*Client) GetCookie

func (c *Client) GetCookie(domain, name string) (value string, err error)

GetCookie gets cookie value by domain and cookie name stdlib cookie jar just export cookie name and value

func (*Client) GetCookies added in v1.3.0

func (c *Client) GetCookies(domain string) (cookies []*http.Cookie, err error)

GetCookies gets []*http.Cookie by domain

func (*Client) ImportCookie added in v1.4.0

func (c *Client) ImportCookie(domain, jsonStr string) (err error)

ImportCookie imports cookie from json

func (*Client) Request

func (c *Client) Request(opt *Option) (res *http.Response, err error)

Request sends http request

func (*Client) SetCookies added in v1.3.0

func (c *Client) SetCookies(domain string, cookies []*http.Cookie) (err error)

SetCookies sets cookies by domain

func (*Client) SetProxy added in v1.4.0

func (c *Client) SetProxy(proxyURLStr string) (err error)

SetProxy sets client proxy

func (*Client) SetTimeout added in v1.3.0

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

SetTimeout sets client timeout

type Data

type Data map[string][]string

Data is the body of http request

type Header map[string]string

Header is the header of http request

type Option

type Option struct {
	Url      string // required
	Method   string // default: "GET", anything "POST", "PUT", "DELETE" or "PATCH"
	BodyStr  string
	Body     *Data
	Form     *Data       // set Content-Type header as "application/x-www-form-urlencoded"
	Json     interface{} // set Content-Type header as "application/json"
	Query    *Data
	QueryRaw string
	Header   *Header
}

Option holds all the #Request requirements

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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