requests

package module
v1.1.2 Latest Latest
Warning

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

Go to latest
Published: Nov 27, 2024 License: MIT Imports: 18 Imported by: 0

README

介绍

一个方便好用的 Go Requests

安装

go get -u github.com/Zcentury/requests

使用

GET

resp := requests.Get("https://www.baidu.com/")

POST

requests.Post("https://example.com", params.BodyString("user=xxx&pass=xxx"))

requests.Post("https://example.com", params.BodyMap2String{
    "user": "xxx",
    "pass": "xxx",
})

requests.Post("https://example.com", params.BodyJsonString("{\"user\":\"xxx\",\"pass\":\"xxx\"}"))

requests.Post("https://example.com", params.BodyMap2Json{
    "user": "xxx",
    "pass": "xxx",
})

自动保存 Cookie

session := requests.Session()

get := session.Get(method.GET, params.Url("http://www.baidu.com/"), headers)
if get != nil {
    fmt.Println(get.Response.Status)
    fmt.Println(get.Response.Cookies())
}

get1 := session.Post("http://www.baidu.com/", headers, params.BodyMap2Json{
    "user": "xxx",
    "pass": "xxx",
})
if get1 != nil {
    fmt.Println(get1.Response.Status)
    fmt.Println(get1.Response.Cookies())
}

使用代理

resp := requests.Get("https://www.baidu.com/", params.Proxy{
    "http":   "http://127.0.0.1:8080",
	"https":  "https://127.0.0.1:8080",
	"socks5": "127.0.0.1:7890",
})

版本更新

  • 2024/11/04:更新了压缩数据包自动解压,去掉内置错误日志,优化项目结构

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrNoUrl  = errors.New("请传入URL")
	ErrNoBody = errors.New("请传入Body")
)

Functions

This section is empty.

Types

type Requests

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

func NewRequests

func NewRequests(client *http.Client) *Requests

func Session

func Session() *Requests

func (*Requests) Delete added in v1.1.0

func (r *Requests) Delete(url string, args ...interface{}) (*Response, error)

func (*Requests) Get

func (r *Requests) Get(url string, args ...interface{}) (*Response, error)

func (*Requests) Post

func (r *Requests) Post(url string, args ...interface{}) (*Response, error)

func (*Requests) Put added in v1.1.0

func (r *Requests) Put(url string, args ...interface{}) (*Response, error)

func (*Requests) Request

func (r *Requests) Request(m method.Method, url string, args ...any) (*Response, error)

type Response

type Response struct {
	Response   *http.Response // 原始 http.Response
	Text       string         // 字符串Body
	StatusCode int            // 状态码
	Body       io.ReadCloser  // Copy过来的Body流,流操作用这个,因为Response的Body已经关闭了
	Header     http.Header    // 响应头
}

func Delete added in v1.0.1

func Delete(url string, args ...interface{}) (*Response, error)

func Get

func Get(url string, args ...interface{}) (*Response, error)

func Post

func Post(url string, args ...interface{}) (*Response, error)

func Put added in v1.0.1

func Put(url string, args ...interface{}) (*Response, error)

func Request

func Request(method method.Method, url string, args ...interface{}) (*Response, error)

Directories

Path Synopsis
examples
get

Jump to

Keyboard shortcuts

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