minireq

package module
Version: v1.0.5 Latest Latest
Warning

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

Go to latest
Published: Jul 8, 2021 License: GPL-3.0 Imports: 16 Imported by: 0

README

minireq

简单的 HTTP 封装,仅包括 Get / Post

Feature

  • Set Headers
  • Set Params
  • Set S5 Proxy
  • HTTP Basic Auth
  • JSON Body
  • Form Body
  • Output Raw Data
  • Output JSON Data
  • Manual Cookies
  • Redirect Switch
  • CookieJar Switch

Example

1. GET
request := minireq.Requests()
res := request.Get(
    "https://httpbin.org/get",
)
fmt.Println(res.RawJSON())
2. Get With Param
request := minireq.Requests()
headers := minireq.Headers{
    "User-Agent": "MyUserAgent",
}
params := minireq.Params{
    "foo": "This is a get!",
}
res := request.Get(
    "https://httpbin.org/get",
    headers,
    params,
)
fmt.Println(res.RawJSON())
3. Get With Auth
request := minireq.Requests()
authData := minireq.Auth{
    "admin",
    "123456",
}
res := request.Get(
    "http://example.com/auth",
    authData,
)
fmt.Println(string(res.RawData()))
4. POST
request := minireq.Requests()
res := request.Post(
    "https://httpbin.org/post",
)
fmt.Println(res.RawJSON())
5. Post By JSON
request := minireq.Requests()
data := minireq.JSONData{
    "username": "admin",
    "password": "123456",
}
res := request.Post(
    "https://httpbin.org/json",
    data,
)
fmt.Println(string(res.RawData()))
6. Post By Form
request := minireq.Requests()
data := minireq.FormData{
    "username": "admin",
    "password": "123456",
}
res := request.Post(
    "https://httpbin.org/json",
    data,
)
fmt.Println(string(res.RawData()))
7. Post Files
request := minireq.Requests()
fdata := minireq.FileData{
    "files[]": []string{
        "go.mod",
        "go.sum",
    },
}
request.Post(
    "http://example.com/files",
    fdata,
)
8. Set Proxy
request := minireq.Requests()
request.Proxy("127.0.0.1:1080")
res := request.Get(
    "https://httpbin.org/get",
)
fmt.Println(string(res.RawData()))
9. Cookies / NoRedirect / NoCookieJar
request := minireq.Requests()
request.NoCookieJar(true)
data := FormData{
    "username": "admin",
    "password": "123456",
}
request.NoRedirect(true)
loginResp := request.Post("http://example.com/login", data)
cookies := loginResp.RawRes.Cookies()
req.SetCookies(cookies)
contentResp := request.Get("http://example.com/home")
fmt.Println(string(contentResp.RawData()))

参考

requests

Documentation

Index

Constants

View Source
const DefaultUA = "MiniRequest/" + DefaultVer

DefaultUA 默认 User-Agent

View Source
const DefaultVer = "1.0"

DefaultVer 版本号

Variables

This section is empty.

Functions

This section is empty.

Types

type Auth

type Auth []string

Auth 设置 HTTP Basic Auth

type FileData

type FileData map[string]interface{}

FileData 设置 File Data

type FormData

type FormData map[string]string

FormData 设置 Form Data

type Headers

type Headers map[string]string

Headers 设置 Header

type JSONData

type JSONData map[string]interface{}

JSONData 设置 JSON Data

type MiniRequest

type MiniRequest struct {
	Request *http.Request
	Header  *http.Header
	Client  *http.Client
}

MiniRequest 提供基本 HTTP 请求

func Requests

func Requests() *MiniRequest

Requests 设置默认的HTTP客户端

1.默认的UserAgent: MiniRequest
2.自动保存Cookies
3.超时时间30秒

func (*MiniRequest) Get

func (mr *MiniRequest) Get(rawURL string, opts ...interface{}) (response *MiniResponse)

Get GET请求

1.获取原始的 Response
2.获取原始的 Request

func (*MiniRequest) NoCookieJar added in v1.0.1

func (mr *MiniRequest) NoCookieJar(s bool)

NoCookieJar 关闭cookiejar

func (*MiniRequest) NoRedirect

func (mr *MiniRequest) NoRedirect(s bool)

NoRedirect 取消自动重定向

func (*MiniRequest) Post

func (mr *MiniRequest) Post(rawURL string, opts ...interface{}) (response *MiniResponse)

Post POST请求

1.获取原始的 Response
2.获取原始的 Request

func (*MiniRequest) Proxy

func (mr *MiniRequest) Proxy(proxyURL string)

Proxy 设置Socks5代理

eg: 127.0.0.1:1080

func (*MiniRequest) SetCookies added in v1.0.1

func (mr *MiniRequest) SetCookies(cookies []*http.Cookie)

SetCookies 设置 Cookies

建议先关闭 NoCookieJar(true) 关闭cookiejar

func (*MiniRequest) TimeOut added in v1.0.1

func (mr *MiniRequest) TimeOut(t int)

TimeOut 设置超时时间 默认30s

type MiniResponse

type MiniResponse struct {
	RawRes *http.Response
	RawReq *MiniRequest
	// contains filtered or unexported fields
}

MiniResponse response

func (*MiniResponse) RawData

func (res *MiniResponse) RawData() []byte

RawData 获取Response的Body

func (*MiniResponse) RawJSON

func (res *MiniResponse) RawJSON() interface{}

RawJSON 获取Response的JSON数据

func (*MiniResponse) RawNumJSON added in v1.0.3

func (res *MiniResponse) RawNumJSON() interface{}

RawNumJSON 获取Response的JSON数据(保留整型大数)

type Params

type Params map[string]string

Params 设置 Params

Source Files

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
t or T : Toggle theme light dark auto
y or Y : Canonical URL