zdpgo_requests

package module
v0.2.7 Latest Latest
Warning

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

Go to latest
Published: May 8, 2022 License: MIT Imports: 16 Imported by: 2

README

zdpgo_requests

Golang中用于发送HTTP请求的库

版本历史

  • v0.1.0 2022/4/9 新增GET和POST请求
  • v0.1.1 2022/4/11 POST的map默认当表单数据
  • v0.1.2 2022/4/11 添加忽略URL解析错误的请求方法
  • v0.1.3 2022/4/12 支持POST纯文本数据
  • v0.1.4 2022/4/12 代码重构
  • v0.1.5 2022/4/13 支持任意类型HTTP请求
  • v0.1.6 2022/4/13 支持设置代理
  • v0.1.7 2022/4/13 支持发送JSON数据
  • v0.1.8 2022/4/16 解决部分URL无法正常请求的BUG
  • v0.1.9 2022/4/18 BUG修复:header请求头重复
  • v0.2.0 2022/4/18 新增:获取请求和响应详情
  • v0.2.1 2022/4/20 新增:获取响应状态码
  • v0.2.2 2022/4/20 新增:下载文件
  • v0.2.3 2022/4/21 新增:文件上传
  • v0.2.4 2022/4/22 新增:支持上传FS文件系统文件
  • v0.2.5 2022/4/28 新增:检查重定向和请求消耗时间
  • v0.2.6 2022/5/6 新增:根据字节数组上传文件
  • v0.2.7 2022/5/8 新增:根据超时时间发送POST请求并携带JSON数据

使用案例

快速入门
package main

import (
	"github.com/zhangdapeng520/zdpgo_requests"
	"github.com/zhangdapeng520/zdpgo_requests/core/requests"
)

func main() {
	// 发送GET请求
	r := zdpgo_New()
	url := "http://localhost:8888"
	resp, err := r.Get(url, true)
	if err != nil {
		return
	}
	println(resp.Text())

	// 发送POST请求
	data := map[string]string{
		"name": "request123",
	}
	resp, _ = r.Post(url, data)
	println(resp.Text())

	// 发送json数据
	var jsonStr Datas = map[string]string{
		"username": "zhangdapeng520",
	}
	var headers Header = map[string]string{
		"Content-Type": "application/json",
	}
	resp, _ = Post(url, true, jsonStr, headers)
	println(resp.Text())

	// 权限校验
	resp, _ = r.Get(
		url,
		Auth{"zhangdapeng520", "password...."},
	)
	println(resp.Text())
}
发送不同类型的请求
package main

import (
	"github.com/zhangdapeng520/zdpgo_requests"
)

func main() {
	r := zdpgo_New()
	targetUrl := "http://localhost:8888"

	// 发送GET请求
	resp, _ := r.Get(targetUrl)
	println(resp.Text())

	// 发送POST请求
	resp, _ = r.Post(targetUrl)
	println(resp.Text())

	// 发送PUT请求
	resp, _ = r.Put(targetUrl)
	println(resp.Text())

	// 发送DELETE请求
	resp, _ = r.Delete(targetUrl)
	println(resp.Text())

	// 发送PATCH请求
	resp, _ = r.Patch(targetUrl)
	println(resp.Text())
}
设置代理
package main

import (
	"fmt"
	"github.com/zhangdapeng520/zdpgo_requests"
)

func main() {
	req := zdpgo_New()

	// 设置代理
	err := req.SetProxy("http://localhost:8888")
	if err != nil {
		panic(err)
	}

	// 发送请求
	// 设置了代理以后,请求被重定向了代理的URL
	resp, _ := req.Get("http://localhost:9999", false)
	fmt.Println("响应:", resp.Text())
}
发送JSON数据
package main

import (
	"github.com/zhangdapeng520/zdpgo_requests"
	"github.com/zhangdapeng520/zdpgo_requests/core/requests"
)

func main() {
	r := zdpgo_New()

	// 发送JSON字符串
	var jsonStr JsonString = "{\"name\":\"requests_post_test\"}"
	resp, _ := r.Post("http://localhost:8888", jsonStr)
	println(resp.Text())

	// 发送map
	var data JsonData = make(map[string]interface{})
	data["name"] = "root"
	data["password"] = "root"
	data["host"] = "localhost"
	data["port"] = 8888
	resp, _ = r.Post("http://localhost:8888", data)
	println(resp.Text())

	// PUT发送JSON数据
	resp, _ = r.Put("http://localhost:8888", data)
	println(resp.Text())

	// DELETE发送JSON数据
	resp, _ = r.Delete("http://localhost:8888", data)
	println(resp.Text())

	// PATCH发送JSON数据
	resp, _ = r.Patch("http://localhost:8888", data)
	println(resp.Text())

	// 发送纯文本数据(非json)
	resp, _ = r.Post("http://localhost:8888", "纯文本内容。。。")
	println(resp.Text())
}
设置请求头
package main

import (
	"github.com/zhangdapeng520/zdpgo_requests"
	"github.com/zhangdapeng520/zdpgo_requests/core/requests"
)

func main() {
	// 直接设置请求头
	req := zdpgo_New()
	req.Request.Header.Set("accept-encoding", "gzip, deflate, br")
	resp, _ := req.Get("http://localhost:8888", false, Header{"Referer": "http://127.0.0.1:9999"})
	println(resp.Text())

	// 将请求头作为参数传递
	h := Header{
		"Referer":         "http://localhost:8888",
		"Accept-Language": "zh-CN,zh;q=0.9,en;q=0.8",
	}
	h2 := Header{
		"Referer":         "http://localhost:8888",
		"Accept-Language": "zh-CN,zh;q=0.9,en;q=0.8",
		"User-Agent":      "zdpgo_requests",
	}
	resp, _ = req.Get("http://localhost:8888", h, h2)
	println(resp.Text())
}
设置查询参数
package main

import (
	"fmt"
	"github.com/zhangdapeng520/zdpgo_requests"
	"github.com/zhangdapeng520/zdpgo_requests/core/requests"
)

func main() {
	req := zdpgo_New()
	p := Params{
		"name": "file",
		"id":   "12345",
	}
	resp, _ := req.Get("http://localhost:8888", false, p)
	fmt.Println(resp.Text())
}
Basic Auth权限校验
package main

import (
	"github.com/zhangdapeng520/zdpgo_requests"
	"github.com/zhangdapeng520/zdpgo_requests/core/requests"
)

func main() {
	req := zdpgo_New()
	resp, _ := req.Get(
		"http://localhost:8080/admin/secrets",
		Auth{"zhangdapeng", "zhangdapeng"},
	)
	println(resp.Text())
}
获取请求和响应详情
package main

import (
	"fmt"
	"github.com/zhangdapeng520/zdpgo_requests"
	"github.com/zhangdapeng520/zdpgo_requests/core/requests"
)

func main() {
	// 发送GET请求
	r := zdpgo_New()
	baseUrl := "http://10.1.3.12:8888/"
	query := "?a=<script>alert(\"XSS\");</script>&b=UNION SELECT ALL FROM information_schema AND ' or SLEEP(5) or '&c=../../../../etc/passwd"
	url := baseUrl + query

	var h1 Header = Header{"a": "111", "b": "222"}
	resp, err := r.GetIgnoreParseError(url, h1)
	if err != nil {
		fmt.Println("错误2", err)
	} else {
		println(resp.Text())
		println("请求详情:\n", resp.RawReqDetail)
		println("响应详情:\n", resp.RawRespDetail)
	}

	var h2 Header = Header{"c": "333", "d": "444"}
	resp1, err := r.GetIgnoreParseError(url, h2)
	if err != nil {
		fmt.Println("错误3", err)
	} else {
		println(resp1.Text())
		println("请求详情:\n", resp.RawReqDetail)
		println("响应详情:\n", resp.RawRespDetail)
	}
}
下载图片
package main

import (
	"github.com/zhangdapeng520/zdpgo_requests"
)

func main() {
	r := zdpgo_New()
	imgUrl := "https://www.twle.cn/static/i/img1.jpg"
	err := r.Download(imgUrl, "test1.jpg")
	if err != nil {
		panic(err)
	}
}
文件上传
package main

import (
	"github.com/zhangdapeng520/zdpgo_requests"
)

func main() {
	r := zdpgo_New()
	imgUrl := "http://localhost:8888/upload"
	err := r.Upload(imgUrl, "test1.jpg")
	if err != nil {
		panic(err)
	}
}
上传FS文件系统文件
package main

import (
	"embed"
	"fmt"
	"github.com/zhangdapeng520/zdpgo_requests"
)

//go:embed test/*
var fsObj embed.FS

func main() {
	r := zdpgo_New()

	targetUrl := "http://localhost:8888/upload"
	filename := "test/main.go"

	respBytes, err := r.UploadFsToBytes(targetUrl, fsObj, "file", filename)
	if err != nil {
		fmt.Println(err)
	}
	fmt.Println(string(respBytes))

	respString, err := r.UploadFsToString(targetUrl, fsObj, "file", filename)
	if err != nil {
		fmt.Println(err)
	}
	fmt.Println(respString)
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var VERSION string = "0.1.6" // 版本编号

Functions

This section is empty.

Types

type Auth added in v0.2.5

type Auth []string // Auth 权限校验类型,{username,password}

type Config added in v0.2.5

type Config struct {
	Timeout       int  `env:"timeout" json:"timeout" yaml:"timeout"`                      // 请求超时时间(秒)
	CheckRedirect bool `env:"check_redirect" json:"check_redirect" yaml:"check_redirect"` // 是否检查重定向
}

type Datas added in v0.2.5

type Datas map[string]string // POST提交的数据

type Files added in v0.2.5

type Files map[string]string // 文件列表:name ,filename
type Header map[string]string // 请求头类型

type JsonData added in v0.2.5

type JsonData map[string]interface{} // 提交JSON格式的数据

type JsonString added in v0.2.5

type JsonString string // 提交JSON格式的字符串

type Params added in v0.2.5

type Params map[string]string // Query查询参数类型

type Request added in v0.2.5

type Request struct {
	Header  *http.Header   // 请求头
	Client  *http.Client   // 请求客户端
	Debug   int            // 是否为DEBUG模式
	Cookies []*http.Cookie // cookie
	Config  *Config        // 配置对象
	// contains filtered or unexported fields
}

Request 请求对象

func NewRequest added in v0.2.5

func NewRequest() *Request

NewRequest 创建请求对象

func NewRequestWithConfig added in v0.2.5

func NewRequestWithConfig(config Config) *Request

func (*Request) Any added in v0.2.5

func (req *Request) Any(method, originUrl string, ignoreParseError bool, args ...interface{}) (resp *Response, err error)

Any 发送任意请求 @param originUrl 要请求的URL地址 @param args 请求携带的参数

func (*Request) ClearCookies added in v0.2.5

func (req *Request) ClearCookies()

ClearCookies 清除cookie

func (*Request) ClientSetCookies added in v0.2.5

func (req *Request) ClientSetCookies()

ClientSetCookies 客户端设置cookie

func (*Request) Close added in v0.2.5

func (req *Request) Close()

Close 关闭连接

func (*Request) Delete added in v0.2.5

func (req *Request) Delete(originUrl string, ignoreParseError bool, args ...interface{}) (resp *Response, err error)

Delete 发送DELETE请求

func (*Request) Get added in v0.2.5

func (req *Request) Get(originUrl string, ignoreParseError bool, args ...interface{}) (resp *Response, err error)

Get 发送GET请求

func (*Request) Patch added in v0.2.5

func (req *Request) Patch(originUrl string, ignoreParseError bool, args ...interface{}) (resp *Response, err error)

Patch 发送PATCH请求

func (*Request) Post added in v0.2.5

func (req *Request) Post(originUrl string, ignoreParseError bool, args ...interface{}) (resp *Response, err error)

Post 发送POST请求 @param originUrl 要请求的URL地址 @param args 请求携带的参数

func (*Request) Proxy added in v0.2.5

func (req *Request) Proxy(proxyUrl string) error

Proxy 设置代理

func (*Request) Put added in v0.2.5

func (req *Request) Put(originUrl string, ignoreParseError bool, args ...interface{}) (resp *Response, err error)

Put 发送PUT请求

func (*Request) SetCookie added in v0.2.5

func (req *Request) SetCookie(cookie *http.Cookie)

SetCookie 设置cookie

func (*Request) SetTimeout added in v0.2.5

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

SetTimeout 设置客户端超时时间(秒)

type Requests

type Requests struct {
	Request *Request // 请求对象
}

func New

func New() *Requests

func (*Requests) AnyJsonWithTimeout added in v0.2.7

func (r *Requests) AnyJsonWithTimeout(method string, targetUrl string, body map[string]interface{},
	timeout int) (result string,
	err error)

AnyJsonWithTimeout 发送任意请求并携带JSON数据

func (*Requests) Delete added in v0.1.5

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

Delete 发送DELETE请求

func (*Requests) DeleteIgnoreParseError added in v0.1.5

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

DeleteIgnoreParseError 发送DELETE请求,且忽略解析URL时遇到的错误

func (*Requests) Download added in v0.2.2

func (r *Requests) Download(urlPath string, savePath string) error

Download 下载文件并保存到指定路径

func (*Requests) DownloadToBytes added in v0.2.2

func (r *Requests) DownloadToBytes(urlPath string) ([]byte, error)

DownloadToBytes 下载文件,返回文件流

func (*Requests) Get

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

Get 发送GET请求

func (*Requests) GetIgnoreParseError added in v0.1.2

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

GetIgnoreParseError 发送GET请求,且忽略解析URL时遇到的错误

func (*Requests) Patch added in v0.1.5

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

Patch 发送PATCH请求

func (*Requests) PatchIgnoreParseError added in v0.1.5

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

PatchIgnoreParseError 发送PATCH请求,且忽略解析URL时遇到的错误

func (*Requests) Post

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

Post 发送POST请求

func (*Requests) PostIgnoreParseError added in v0.1.2

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

PostIgnoreParseError 发送POST请求,且忽略解析URL时遇到的错误

func (*Requests) PostJsonWithTimeout added in v0.2.7

func (r *Requests) PostJsonWithTimeout(targetUrl string, body map[string]interface{}, timeout int) (result string, err error)

PostJsonWithTimeout 发送JSON请求并携带JSON数据

func (*Requests) Put added in v0.1.5

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

Put 发送PUT请求

func (*Requests) PutIgnoreParseError added in v0.1.5

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

PutIgnoreParseError 发送PUT请求,且忽略解析URL时遇到的错误

func (*Requests) SetProxy added in v0.1.6

func (r *Requests) SetProxy(proxyUrl string) error

SetProxy 设置代理

func (*Requests) Upload added in v0.2.3

func (r *Requests) Upload(targetUrl string, filePath string) error

Upload 上传文件 @param targetUrl 目标地址 @param filePath 上传文件的路径 @return 错误对象

func (*Requests) UploadByBytes added in v0.2.6

func (r *Requests) UploadByBytes(targetUrl string, formName string, filePath string, content []byte) ([]byte,
	error)

UploadByBytes 根据字节数组上传文件

func (*Requests) UploadFsToBytes added in v0.2.4

func (r *Requests) UploadFsToBytes(targetUrl string, fsObj fs.FS, fileFormName, filePath string) (result []byte,
	err error)

UploadFsToBytes 上传FS文件系统的文件,返回bytes数据

func (*Requests) UploadFsToString added in v0.2.4

func (r *Requests) UploadFsToString(targetUrl string, fsObj fs.FS, fileFormName, filePath string) (result string,
	err error)

UploadFsToString 上传FS系统文件,并将结果转换为字符串返回

func (*Requests) UploadToBytes added in v0.2.3

func (r *Requests) UploadToBytes(targetUrl string, filePath string) ([]byte, error)

UploadToBytes 上传文件并返回响应体字节数组 @param targetUrl 目标地址 @param filePath 上传文件的路径 @return 响应内容,错误对象

func (*Requests) UploadToResponse added in v0.2.3

func (r *Requests) UploadToResponse(targetUrl string, formName string, filePath string) (*http.Response, error)

UploadToResponse 上传文件并获取响应 @param targetUrl 目标地址 @param formName 文件表单名称 @param filePath 上传文件的路径 @return 响应对象,错误对象

func (*Requests) UploadToString added in v0.2.4

func (r *Requests) UploadToString(targetUrl string, filePath string) (string, error)

UploadToString 上传文件并返回响应字符串 @param targetUrl 目标地址 @param filePath 上传文件的路径 @return 响应内容,错误对象

type Response added in v0.2.5

type Response struct {
	R *http.Response // 响应对象

	RawReqDetail     string // 请求详情字符串
	RawRespDetail    string // 响应详情字符串
	StatusCode       int    // 状态码
	IsRedirect       bool   // 是否重定向了
	RedirectUrl      string // 重定向的的URL地址
	StartTime        int    // 请求开始时间(纳秒)
	EndTime          int    // 请求结束时间(纳秒)
	SpendTime        int    // 请求消耗时间(纳秒)
	SpendTimeSeconds int    // 请求消耗时间(秒)
	// contains filtered or unexported fields
}

Response 响应对象

func Delete added in v0.2.5

func Delete(originUrl string, ignoreParseError bool, args ...interface{}) (resp *Response, err error)

Delete 发送DELETE请求 @param originUrl 要请求的URL地址 @param args 请求携带的参数

func Get added in v0.2.5

func Get(originUrl string, ignoreParseError bool, args ...interface{}) (resp *Response, err error)

Get 发送GET请求 @param originUrl 要请求的URL地址 @param args 请求携带的参数

func Patch added in v0.2.5

func Patch(originUrl string, ignoreParseError bool, args ...interface{}) (resp *Response, err error)

Patch 发送PATCH请求 @param originUrl 要请求的URL地址 @param args 请求携带的参数

func Post added in v0.2.5

func Post(url string, ignoreParseError bool, args ...interface{}) (resp *Response, err error)

Post 发送POST请求 @param url 要请求的URL路径 @param ignoreParseError 是否忽略解析URL错误 @param args 要携带的参数

func Put added in v0.2.5

func Put(originUrl string, ignoreParseError bool, args ...interface{}) (resp *Response, err error)

Put 发送PUT请求 @param originUrl 要请求的URL地址 @param args 请求携带的参数

func (*Response) Content added in v0.2.5

func (resp *Response) Content() []byte

Content 获取响应内容

func (*Response) Cookies added in v0.2.5

func (resp *Response) Cookies() (cookies []*http.Cookie)

Cookies 获取响应的cookie

func (*Response) Json added in v0.2.5

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

Json 解析响应内容为json数据

func (*Response) SaveFile added in v0.2.5

func (resp *Response) SaveFile(filename string) error

SaveFile 保存文件

func (*Response) Text added in v0.2.5

func (resp *Response) Text() string

Text 获取响应文本

Jump to

Keyboard shortcuts

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