curl

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

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

Go to latest
Published: Nov 19, 2019 License: Apache-2.0 Imports: 17 Imported by: 0

README

curl 使用指南

  1. curl 支持自定义 cookie、method、referer;可以自由定义headers
  2. post请求,data域可以传入多种格式数据。

go get -u github.com/Jayden-Max/curl

for example:



Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CURL

type CURL struct {
	// -H 添加http请求标头
	URL     string
	Method  string
	Cookie  string
	Referer string

	Headers map[string]string // header
	Options map[string]bool

	// --data
	PostBytes        []byte               // binary data
	PostString       string               // string data
	PostFields       neturl.Values        // k-v format
	PostReader       io.Reader            // from reader
	PostFieldReaders map[string]io.Reader // from map reader
	PostFiles        neturl.Values        // key 文件名;value 文件路径

	Timeout time.Duration // ctx 超时时间
	// contains filtered or unexported fields
}

refer: http://www.ruanyifeng.com/blog/2011/09/curl.html http://www.ruanyifeng.com/blog/2019/09/curl-reference.html

func NewCurl

func NewCurl(method, url string) *CURL

func (*CURL) CreateRequest

func (c *CURL) CreateRequest() (request *http.Request, err error)

return http's request and set headers.

func (*CURL) Do

func (c *CURL) Do(ctx context.Context) (resp *Response, err error)

start Do http request

if ctx is nil then set ctx equal context.Background() and meantime set timeout 5 second.

func (*CURL) HandleResponse

func (c *CURL) HandleResponse(resp *http.Response) (response *Response, err error)

func (*CURL) SetContext

func (c *CURL) SetContext(ctx context.Context)

func (*CURL) SetCookie

func (c *CURL) SetCookie(cookie string)

func (*CURL) SetDefaultHeaders

func (c *CURL) SetDefaultHeaders()

func (*CURL) SetHeader

func (c *CURL) SetHeader(key, value string)

func (*CURL) SetMethod

func (c *CURL) SetMethod(method string) error

func (*CURL) SetOption

func (c *CURL) SetOption(key string, value bool)

func (*CURL) SetPostBytes

func (c *CURL) SetPostBytes(p []byte)

bytes for data content

func (*CURL) SetPostFieldReaders

func (c *CURL) SetPostFieldReaders(p map[string]io.Reader)

field's io.Reader for data content

func (*CURL) SetPostFields

func (c *CURL) SetPostFields(p neturl.Values)

map[string][]string for data content

func (*CURL) SetPostFiles

func (c *CURL) SetPostFiles(p neturl.Values)

file data for content

func (*CURL) SetPostReader

func (c *CURL) SetPostReader(p io.Reader)

io.Reader for data content

func (*CURL) SetPostString

func (c *CURL) SetPostString(p string)

string for data content

func (*CURL) SetTimeout

func (c *CURL) SetTimeout(sec time.Duration)

func (*CURL) SetUrl

func (c *CURL) SetUrl(url string)

type Response

type Response struct {
	Headers      map[string]string `json:"headers"`
	Cookie       string            `json:"Cookie"`
	URL          string            `json:"URL"`
	Body         string            `json:"body"`
	HttpResponse *http.Response
	BodyReader   io.ReadCloser
}

func (*Response) Close

func (r *Response) Close()

func (*Response) ReadBody

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

Jump to

Keyboard shortcuts

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