requests

package module
v2.0.2 Latest Latest
Warning

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

Go to latest
Published: May 19, 2022 License: Apache-2.0 Imports: 15 Imported by: 0

README

Requests

下载

go get -u gitee.com/ropon/requests/v2

使用

//构建一个新对象
req := requests.New()
req.Get("url")
//或者直接使用默认对象请求
requests.Get("url")

//构建请求头
req.Headers = map[string]string{
    "User-Agent": `这里填写自定义UA信息`,
}
req.Header()

//构建Cookie
req.Cookies = map[string]string{
    "key": "val",
}
req.Cookie()

GET:

//无参数Get请求
res, err := req.Get("https://www.ropon.top")
//错误处理
if err != nil {
    log.Fatal(err)
}
fmt.Println(res.Text())

//有参数Get请求
queryData := map[string]interface{}{
    "key": "val",
    "key2": 123,
}
res, err := req.Get("https://www.ropon.top", queryData)
//错误处理
if err != nil {
    log.Fatal(err)
}
fmt.Println(res.Text())

POST:

//默认urlencode编码
postData := map[string]interface{}{
    "key": "val",
	"key2": 123,
}
res, err := req.Post("https://www.ropon.top", postData)
//错误处理
if err != nil {
    log.Fatal(err)
}
fmt.Println(res.Text())

//请求传json
postJsonStr := `{"key": "val"}`
res, err := req.Post("https://www.ropon.top", postJsonStr)
//错误处理
if err != nil {
    log.Fatal(err)
}
fmt.Println(res.Text())

PUT:

//默认urlencode编码
putData := map[string]interface{}{
    "key": "val",
	"key2": 123,
}
res, err := req.Put("https://www.ropon.top", putData)
//错误处理
if err != nil {
    log.Fatal(err)
}
fmt.Println(res.Text())

//请求传json
putJsonStr := `{"key": "val"}`
res, err := req.Put("https://www.ropon.top", putJsonStr)
//错误处理
if err != nil {
    log.Fatal(err)
}
fmt.Println(res.Text())

PATCH:

//默认urlencode编码
patchData := map[string]interface{}{
    "key": "val",
	"key2": 123,
}
res, err := req.Patch("https://www.ropon.top", patchData)
//错误处理
if err != nil {
    log.Fatal(err)
}
fmt.Println(res.Text())

//请求传json
patchJsonStr := `{"key": "val"}`
res, err := req.Patch("https://www.ropon.top", patchJsonStr)
//错误处理
if err != nil {
    log.Fatal(err)
}
fmt.Println(res.Text())

DELETE:

//默认urlencode编码
deleteData := map[string]interface{}{
    "key": "val",
    "key2": 123,
}
res, err := req.Delete("https://www.ropon.top", deleteData)
//错误处理
if err != nil {
    log.Fatal(err)
}
fmt.Println(res.Text())

//请求传json
deleteJsonStr := `{"key": "val"}`
res, err := req.Delete("https://www.ropon.top", deleteJsonStr)
//错误处理
if err != nil {
    log.Fatal(err)
}
fmt.Println(res.Text())

Cookies:

//单独带cookie请求
req.Cookies["key1"] = "val1"
req.Cookie()
res, err := req.Get("https://www.ropon.top")

Headers:

//单独带header请求
req.Headers["key1"] = "val1"
req.Header()
res, err := req.Get("https://www.ropon.top")

Res:

//获取文本信息
res.Text()
//获取Json,f返回requests.Value
res.Json()
res.Json().Get("data", "svc_list").String()
//获取响应头
res.Header()
//获取状态码
res.Status()

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Struct2Map

func Struct2Map(obj interface{}, tagName string) map[string]interface{}

Struct2Map 结构体转map,传入结构体

func StructPtr2Map

func StructPtr2Map(obj interface{}, tagName string) map[string]interface{}

StructPtr2Map 结构体指针转map,传入结构体指针

Types

type Request

type Request struct {
	Headers map[string]string
	Cookies map[string]string
	Params  url.Values
	// contains filtered or unexported fields
}

Request 请求相关

func New

func New(options ...bool) *Request

New 构造方法

func (*Request) BaseReq

func (req *Request) BaseReq(Method, urlStr string, options ...interface{}) (resp *Response, err error)

func (*Request) Cookie

func (req *Request) Cookie()

Cookie 管理

func (*Request) Delete

func (req *Request) Delete(urlStr string, options ...interface{}) (resp *Response, err error)

Delete delete方法

func (*Request) Do

func (req *Request) Do() (*Response, error)

func (*Request) EnableCookie

func (req *Request) EnableCookie(enable bool)

func (*Request) Get

func (req *Request) Get(urlStr string, options ...interface{}) (resp *Response, err error)

Get get方法

func (*Request) Header

func (req *Request) Header()

Header 请求头

func (*Request) Patch

func (req *Request) Patch(urlStr string, options ...interface{}) (resp *Response, err error)

Patch put方法

func (*Request) Post

func (req *Request) Post(urlStr string, options ...interface{}) (resp *Response, err error)

Post post方法

func (*Request) Put

func (req *Request) Put(urlStr string, options ...interface{}) (resp *Response, err error)

Put put方法

func (*Request) SetBasicAuth

func (req *Request) SetBasicAuth(username, password string)

设置基本认证

func (*Request) SetTimeout

func (req *Request) SetTimeout(n time.Duration)

SetTimeout 设置超时时间

type Response

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

Response 响应相关

func Delete

func Delete(urlStr string, options ...interface{}) (resp *Response, err error)

func Get

func Get(urlStr string, options ...interface{}) (resp *Response, err error)

func Patch

func Patch(urlStr string, options ...interface{}) (resp *Response, err error)

func Post

func Post(urlStr string, options ...interface{}) (resp *Response, err error)

func Put

func Put(urlStr string, options ...interface{}) (resp *Response, err error)

func (*Response) Body

func (res *Response) Body() (body io.ReadCloser)

func (*Response) Content

func (res *Response) Content() (content []byte)

func (*Response) Cookie

func (res *Response) Cookie() []*http.Cookie

Cookie 响应信息

func (*Response) Header

func (res *Response) Header() map[string][]string

Header 响应头信息

func (*Response) Json

func (res *Response) Json() Value

func (*Response) RawJson

func (res *Response) RawJson(v interface{}) error

func (*Response) SetRes

func (res *Response) SetRes(rawRes *http.Response)

func (*Response) Status

func (res *Response) Status() int

Status 响应状态码

func (*Response) Text

func (res *Response) Text() (text string)

Text 返回文本信息

type Value

type Value struct {
	Data   interface{}
	Exists bool
}

func NewJson

func NewJson(data []byte) (Value, error)

func (Value) Bool

func (v Value) Bool() bool

func (Value) Float64

func (v Value) Float64() float64

func (Value) Get

func (v Value) Get(path ...interface{}) Value

func (Value) Int64

func (v Value) Int64() int64

func (Value) Map

func (v Value) Map() map[string]interface{}

func (Value) Size

func (v Value) Size() int

func (Value) String

func (v Value) String() string

func (Value) StringArray

func (v Value) StringArray(path ...interface{}) []string

func (Value) Time

func (v Value) Time(layout string, options ...string) time.Time

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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