package module
v0.0.0-...-8c3df60 Latest Latest

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

Go to latest
Published: Jan 6, 2022 License: Apache-2.0 Imports: 32 Imported by: 0


urlx: http请求的封装, 链式API




View Source
const (
	HeaderAccept         = "Accept"
	HeaderAcceptLanguage = "Accept-Language"
	HeaderAcceptEncoding = "Accept-Encoding"
	HeaderUserAgent      = "User-Agent"
	HeaderContentType    = "Content-Type"
	HeaderReferer        = "Referer"
	HeaderCacheControl   = "Cache-Control" // no-cache
	HeaderPragma         = "Pragma"        // no-cache
View Source
const (
	MethodGet     = http.MethodGet
	MethodHead    = http.MethodHead
	MethodPost    = http.MethodPost
	MethodPut     = http.MethodPut
	MethodPatch   = http.MethodPatch
	MethodDelete  = http.MethodDelete
	MethodConnect = http.MethodConnect
	MethodOptions = http.MethodOptions
	MethodTrace   = http.MethodTrace
View Source
const HeaderContentEncoding = "Content-Encoding"
View Source
const Version = "0.0.1"


View Source
var (
	// MacEdgeAgent Mac Edge 浏览器的 UserAgent
	MacEdgeAgent = UserAgent("Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.55 Safari/537.36 Edg/96.0.1054.43")

	// IPhoneAgent IPhone Edge 浏览器的 UserAgent
	IPhoneAgent = UserAgent("Mozilla/5.0 (iPhone; CPU iPhone OS 13_2_3 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0.3 Mobile/15E148 Safari/604.1 Edg/96.0.4664.55")

	// WindowsEdgeAgent Windows Edge 浏览器的 UserAgent
	WindowsEdgeAgent = UserAgent("Mozilla/5.0 (Windows NT 10.0; Win64; x64; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.55 Safari/537.36 Edg/96.0.1054.43")
View Source
var (
	// NoCache 无缓存
	NoCache = HeaderOption(func(headers http.Header) {
		headers.Set(HeaderCacheControl, "no-cache")
		headers.Set(HeaderPragma, "no-cache")

	// AcceptAllEncodings 接受所有的编码格式
	AcceptAllEncodings = AcceptEncoding("zstd", "br", "gzip", "deflate", "snappy", "s2")

	// DefaultEncodings 默认接受所有的编码格式
	DefaultEncodings = AcceptEncoding("gzip", "deflate", "br")

	// AcceptChinese 接受中文
	AcceptChinese = AcceptLanguage("zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6,zh-TW;q=0.5")

	// AcceptHTML 接受网页浏览器格式
	AcceptHTML = Accept("text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9")

	// AcceptJSON 接受JSON格式
	AcceptJSON = Accept("application/json")

	// AcceptXML 接受XML格式
	AcceptXML = Accept("application/xml,text/xml")

	// AcceptAny 接受任意格式
	AcceptAny = Accept("*/*")
View Source
var ProcessNil = func(resp *http.Response, body io.ReadCloser) error { return nil }


This section is empty.


type Body

type Body = func() (contentType string, body io.Reader, err error) // 请求提交内容构造方法


type HeaderOption

type HeaderOption = func(headers http.Header) // 请求头处理


var DefaultUserAgent HeaderOption

func Accept

func Accept(accept string) HeaderOption

Accept 接受格式

func AcceptEncoding

func AcceptEncoding(acceptEncodings ...string) HeaderOption

AcceptEncoding 接受编码

func AcceptLanguage

func AcceptLanguage(acceptLanguages ...string) HeaderOption

AcceptLanguage 接受语言

func HeaderDel

func HeaderDel(keys ...string) HeaderOption

HeaderDel 删除请求头

func HeaderSet

func HeaderSet(key string, values ...string) HeaderOption

HeaderSet 设置请求头

func Referer

func Referer(referer string) HeaderOption

Referer 引用地址

func UserAgent

func UserAgent(userAgent string) HeaderOption

UserAgent 浏览器代理字符串

type MultipartBody

type MultipartBody struct {
	// contains filtered or unexported fields

func Multipart

func Multipart() *MultipartBody

func (*MultipartBody) Body

func (m *MultipartBody) Body() (contentType string, body io.Reader, err error)

func (*MultipartBody) File

func (m *MultipartBody) File(getFile func() (field, filename string, fileBody io.ReadCloser, err error)) *MultipartBody

func (*MultipartBody) LocalFile

func (m *MultipartBody) LocalFile(field string, filename string) *MultipartBody

func (*MultipartBody) Params

func (m *MultipartBody) Params(params url.Values) *MultipartBody

func (*MultipartBody) WaitEnd

func (m *MultipartBody) WaitEnd(ctx context.Context) error

type Option

type Option = func(*Request) error // 请求选项


func CookieEnabled

func CookieEnabled(enabled ...bool) Option

CookieEnabled 开关 Cookie

func Jar

func Jar(jar http.CookieJar) Option

Jar 设置Cookie容器

func UseClient

func UseClient(client *http.Client) Option

UseClient 使用自定义的HTTP客户端

type Process

type Process = func(resp *http.Response, body io.ReadCloser) error // 响应处理器

func CharsetDecode

func CharsetDecode(next Process) Process

func DecompressionBody

func DecompressionBody(next Process) Process

DecompressionBody 解压Body

func JSON

func JSON(out any) Process

JSON 处理响应

func XML

func XML(out any) Process

XML 处理响应

func YAML

func YAML(out any) Process

YAML 处理响应

type ProcessMw

type ProcessMw = func(next Process) Process // 响应预处理器

func Status

func Status(processStatus func(status int) Process) ProcessMw

Status .

type Request

type Request struct {
	// contains filtered or unexported fields

Request 请求构造

func Default

func Default(ctx context.Context) *Request

Default 默认的请求器

func IPhoneEdge

func IPhoneEdge(ctx context.Context) *Request

IPhoneEdge IPhone Edge 浏览器

func MacEdge

func MacEdge(ctx context.Context) *Request

MacEdge Mac Edge 浏览器

func New

func New(ctx context.Context, options ...Option) *Request

New 以一些选项开始初始化请求器

func NewBrowser

func NewBrowser(ctx context.Context, userAgent HeaderOption) *Request

NewBrowser 浏览器

func WindowsEdge

func WindowsEdge(ctx context.Context) *Request

WindowsEdge Windows Edge 浏览器

func (*Request) Bytes

func (c *Request) Bytes() (data []byte, err error)

Bytes 处理响应字节

func (*Request) Download

func (c *Request) Download(fn string) (err error)

Download 下载到文件

func (*Request) HeaderWith

func (c *Request) HeaderWith(options ...HeaderOption) *Request

HeaderWith 设置请求头

func (*Request) JSON

func (c *Request) JSON(out any) (data []byte, err error)

JSON 处理响应

func (*Request) Method

func (c *Request) Method(method string) *Request

Method 设置请求方法

func (*Request) Process

func (c *Request) Process(process Process) error

Process 处理响应

func (*Request) ProcessWith

func (c *Request) ProcessWith(mws ...ProcessMw) *Request

ProcessWith 在处理之前的预处理

func (*Request) Query

func (c *Request) Query(query string) *Request

Query 设置请求Query参数

func (*Request) SendBody

func (c *Request) SendBody(body Body) *Request

SendBody 设置请求提交内容

func (*Request) SendForm

func (c *Request) SendForm(v any) *Request

SendForm 提交表单

func (*Request) SendJSON

func (c *Request) SendJSON(v any) *Request


func (*Request) SendXML

func (c *Request) SendXML(v any) *Request


func (*Request) TryAt

func (c *Request) TryAt(times ...time.Duration) *Request

TryAt 失败重试,等待休眠时间

func (*Request) Url

func (c *Request) Url(url string) *Request

Url 设置请求链接

func (*Request) UseClient

func (c *Request) UseClient(client *http.Client) *Request

UseClient 使用的客户端定义

func (*Request) With

func (c *Request) With(options ...Option) *Request

With 增加选项

func (*Request) XML

func (c *Request) XML(out any) (data []byte, err error)

XML 处理响应

func (*Request) YAML

func (c *Request) YAML(out any) (data []byte, err error)

YAML 处理响应


Path Synopsis
htmlquery module

Jump to

Keyboard shortcuts

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