requests

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: May 13, 2019 License: Apache-2.0 Imports: 19 Imported by: 1

README

request

Golang HTTP Requests for Humans™ ✨🍰✨

Usage
  • 基本用法

requests.Get("https://golang.org)
requests.Post("https://golang.org, "application/json", `{"a": "b"}`)
  • 高级用法
package main

import (
    "log"

    "github.com/luopengift/requests"
)

func main() {
    sess := requests.New()                                           // 创建session
    sess.SetHeader("aaa", "bbb")                                     // 全局配置, 会追加到使用这个sess的所有请求中
    req, err := requests.NewRequest("GET", "http://httpbin.org", nil) // 创建一个GET请求
    if err != nil {
        log.Fatal(err)
        return
    }
    req.SetHeader("foo", "bar") // req的参数设置会覆盖sess中的参数
    resp, err := sess.DoRequest(req) //发送创建的请求
    if err != nil {
        log.Fatal(err)
        return
    }
    _, err = resp.Text() //解析响应
    if err != nil {
        log.Fatal(err)
        return
    }
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrEmptyProxy = errors.New("proxy is empty")
)

var

Functions

func DumpRequest

func DumpRequest(req *http.Request) ([]byte, error)

DumpRequest returns the given request in its HTTP/1.x wire representation.

func DumpRequestIndent

func DumpRequestIndent(req *http.Request) string

DumpRequestIndent warp Dump

func SetLogFunc

func SetLogFunc(f func(string, ...interface{}))

SetLogFunc set log handler

func Wget

func Wget(url, name string) (int, error)

Wget download a file from remote.

Types

type Request

type Request struct {
	Method  string
	URL     string
	Params  map[string]interface{}
	Headers map[string]string
	Cookies map[string]string
	Body    []byte
	Form    url.Values
	Retry   int
}

Request request

func NewRequest

func NewRequest(method, urlstr string, body io.Reader) *Request

NewRequest new request

func (*Request) MergeIn

func (req *Request) MergeIn(r *Request)

MergeIn merge r into req

func (*Request) Request

func (req *Request) Request() (*http.Request, error)

Request request

func (*Request) SetAuth

func (req *Request) SetAuth(user, pass string) *Request

SetAuth base auth

func (*Request) SetBody

func (req *Request) SetBody(body interface{}) error

SetBody request body

func (*Request) SetCookie

func (req *Request) SetCookie(k, v string) *Request

SetCookie cookie

func (*Request) SetCookies

func (req *Request) SetCookies(kv map[string]string) *Request

SetCookies cookie

func (*Request) SetForm

func (req *Request) SetForm(k, v string) *Request

SetForm set form, content-type is

func (*Request) SetHeader

func (req *Request) SetHeader(k, v string) *Request

SetHeader header

func (*Request) SetHeaders

func (req *Request) SetHeaders(kv map[string]string) *Request

SetHeaders headers

func (*Request) SetMethod

func (req *Request) SetMethod(method string) *Request

SetMethod set method

func (*Request) SetParam

func (req *Request) SetParam(k string, v interface{}) *Request

SetParam params

func (*Request) SetParams

func (req *Request) SetParams(query map[string]interface{}) *Request

SetParams add query args

func (*Request) SetRetry

func (req *Request) SetRetry(retry int) *Request

SetRetry set retry

func (*Request) SetURL

func (req *Request) SetURL(url string) *Request

SetURL set url

type Response

type Response struct {
	*http.Response
	// contains filtered or unexported fields
}

Response wrap std response

func Delete

func Delete(url string, body interface{}) (resp *Response, err error)

Delete send post request

func Get

func Get(url string) (*Response, error)

Get send get request

func Head(url string) (resp *Response, err error)

Head send post request

func PUT

func PUT(url string, body interface{}) (resp *Response, err error)

PUT send post request

func Post

func Post(url, contentType string, body interface{}) (*Response, error)

Post send post request

func PostForm

func PostForm(url string, data url.Values) (resp *Response, err error)

PostForm send post request, content-type = application/x-www-form-urlencoded

func WarpResponse

func WarpResponse(resp *http.Response) *Response

WarpResponse warp response

func (*Response) Copy

func (resp *Response) Copy() *Response

Copy deep copy response

func (*Response) Download

func (resp *Response) Download(name string) (int, error)

Download parse response to a file

func (*Response) Dump

func (resp *Response) Dump() ([]byte, error)

Dump returns the given request in its HTTP/1.x wire representation.

func (*Response) DumpIndent

func (resp *Response) DumpIndent() string

DumpIndent warp Dump

func (*Response) JSON

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

JSON parse response

func (*Response) StdLib

func (resp *Response) StdLib() *http.Response

StdLib return net/http.Response

func (*Response) Text

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

Text parse parse to string

type Session

type Session struct {
	*http.Transport
	*http.Client

	LogFunc func(string, ...interface{})
	// contains filtered or unexported fields
}

Session httpclient session Clients and Transports are safe for concurrent use by multiple goroutines for efficiency should only be created once and re-used. so, session is also safe for concurrent use by multiple goroutines.

func New

func New() *Session

New new session

func (*Session) DebugTrace

func (sess *Session) DebugTrace(request *Request)

DebugTrace trace a request

sess := requests.New()
req, err := requests.NewRequest("GET", "http://www.baidu.com", nil)
if err != nil {
	sess.LogFunc("%v", err)
}
sess.DebugTrace(req)

* Connect: www.baidu.com:80 * Resolved Host: www.baidu.com * Resolved DNS: [14.215.177.39 14.215.177.38], Coalesced: false, err=<nil> * Trying tcp 14.215.177.39:80... * Completed connection: tcp 14.215.177.39:80, err=<nil> * Got Conn: {0xc0000ac020 false false 0s} > GET / HTTP/1.1 > Host: www.baidu.com > User-Agent: Go-http-client/1.1 > Accept-Encoding: gzip > > < HTTP/1.1 200 OK < Transfer-Encoding: chunked < Bdpagetype: 1 < Bdqid: 0x85d6ecb5000fcc70 ...more...

func (*Session) Delete

func (sess *Session) Delete(url, contentType string, body io.Reader) (resp *Response, err error)

Delete send delete request

func (*Session) DeleteWithContext

func (sess *Session) DeleteWithContext(ctx context.Context, url, contentType string, body io.Reader) (resp *Response, err error)

DeleteWithContext send delete request

func (*Session) Do

func (sess *Session) Do(method, url, contentType string, body io.Reader) (*Response, error)

Do http request

func (*Session) DoRequest

func (sess *Session) DoRequest(request *Request, ctx ...context.Context) (*Response, error)

DoRequest send a request and return a response

func (*Session) DoWithContext

func (sess *Session) DoWithContext(ctx context.Context, method, url, contentType string, body io.Reader) (*Response, error)

DoWithContext http request

func (*Session) Get

func (sess *Session) Get(url string) (*Response, error)

Get send get request

func (*Session) GetWithContext

func (sess *Session) GetWithContext(ctx context.Context, url string) (*Response, error)

GetWithContext http request

func (*Session) Post

func (sess *Session) Post(url, contentType string, body io.Reader) (resp *Response, err error)

Post send post request

func (*Session) PostForm

func (sess *Session) PostForm(url string, data url.Values) (resp *Response, err error)

PostForm post form request

func (*Session) PostFormWithContext

func (sess *Session) PostFormWithContext(ctx context.Context, url string, data url.Values) (resp *Response, err error)

PostFormWithContext post form request

func (*Session) PostWithContext

func (sess *Session) PostWithContext(ctx context.Context, url, contentType string, body io.Reader) (resp *Response, err error)

PostWithContext send post request

func (*Session) Put

func (sess *Session) Put(url, contentType string, body io.Reader) (resp *Response, err error)

Put send put request

func (*Session) PutWithContext

func (sess *Session) PutWithContext(ctx context.Context, url, contentType string, body io.Reader) (resp *Response, err error)

PutWithContext send put request

func (*Session) SetAuth

func (sess *Session) SetAuth(user, pass string) *Session

SetAuth base auth, proxy is Proxy-Authorization

func (*Session) SetCookie

func (sess *Session) SetCookie(k, v string) *Session

SetCookie cookie

func (*Session) SetHeader

func (sess *Session) SetHeader(k, v string) *Session

SetHeader header

func (*Session) SetHeaders

func (sess *Session) SetHeaders(kv map[string]string) *Session

SetHeaders headers

func (*Session) SetKeepAlives

func (sess *Session) SetKeepAlives(keepAlives bool) *Session

SetKeepAlives set transport disableKeepAlives default transport is keepalive, if set false, only use the connection to the server for a single HTTP request.

func (*Session) SetLogFunc

func (sess *Session) SetLogFunc(f func(string, ...interface{})) *Session

SetLogFunc set log handler

func (*Session) SetParam

func (sess *Session) SetParam(k string, v interface{}) *Session

SetParam query

func (*Session) SetParams

func (sess *Session) SetParams(kv map[string]interface{}) *Session

SetParams querys

func (*Session) SetProxy

func (sess *Session) SetProxy(addr string) error

SetProxy set proxy addr os.Setenv("HTTP_PROXY", "http://127.0.0.1:9743") os.Setenv("HTTPS_PROXY", "https://127.0.0.1:9743")

func (*Session) SetRetry

func (sess *Session) SetRetry(retry int, retryFunc ...func(Response, error) error) *Session

SetRetry set retry times if request fail

func (*Session) SetTimeout

func (sess *Session) SetTimeout(timeout int) *Session

SetTimeout set client timeout

Jump to

Keyboard shortcuts

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