http

package
v1.0.14 Latest Latest
Warning

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

Go to latest
Published: Mar 13, 2024 License: Apache-2.0 Imports: 10 Imported by: 0

README

HTTP

封装http请求

import (
  "fmt"
  "gitee.com/novanest/toolkit/http"
)

func main() {
    func main() {
  h := http.Header("Authorization", "Bearer xxxxxxxxxxxxxxx").
    UserAgent("your custom user-agent").
    Cookie().
    BaseURL("https://127.0.0.1")

  res, err := h.Get("/v1/webapi/test", map[string]string{
    "param": 1,
  })

  // 作为字符串获取返回结果
  fmt.Printf("print string: %s\n", res.String())
  // 作为字节获取返回结果
  fmt.Printf("print bytes: %v", res.Byte())

  // 返回结果绑定结构体
  s := []struct {
		URL   string `json:"url"`
		Title string `json:"title"`
  }{}
  res.JSON(&s)
  fmt.Printf("print Struct: %v", s)

  // 使用代理
  res, err := http.Proxy("http://127.0.0.1").Get("https://127.0.0.2", map[string]string{
		"sf": "ace",
  })
  fmt.Printf("返回结果: %s", res.String())
}

http server


config := Config{
	Handler:          nil,// 如果 Handler 字段的值为 nil,则会调用 http.DefaultServeMux 来处理请求
	ReadHeaderTimeout: 15, // 读取请求头的超时时间
	WebAddr           :"127.0.0.1" // 监听地址 0.0.0.0:8080
	Ishttps : true, // 是否是https
	CertFile : "/temp/certfile"// 证书文件
	KeyFile : "/temp/keyfile"// 私钥文件
}
// 启动服务
if err := config.Server(); err != nil {
  panic(err)
}

Documentation

Index

Constants

View Source
const (
	TypeJSON       = "json"
	TypeXML        = "xml"
	TypeUrlencoded = "urlencoded"
	TypeForm       = "form"
	TypeFormData   = "form-data"
	TypeHTML       = "html"
	TypeText       = "text"
	TypeMultipart  = "multipart"
)

Variables

View Source
var Types = map[string]string{
	TypeJSON:       "application/json",
	TypeXML:        "application/xml",
	TypeForm:       "application/x-www-form-urlencoded",
	TypeFormData:   "application/x-www-form-urlencoded",
	TypeUrlencoded: "application/x-www-form-urlencoded",
	TypeHTML:       "text/html",
	TypeText:       "text/plain",
	TypeMultipart:  "multipart/form-data",
}

Functions

This section is empty.

Types

type Request

type Request struct {
	SuperAgent *SuperAgent
	// contains filtered or unexported fields
}

Request 请求结构

func BaseURL

func BaseURL(url string) Request

BaseURL 设置url前缀

func Cookie(c *http.Cookie) Request

Cookie 设置请求 Cookie

func Header(key, val string) Request

Header 设置请求 Header

func New

func New() Request

New 新建请求对象,默认数据类型 json

func Proxy

func Proxy(url string) Request

Proxy 设置请求代理

func Query

func Query(query interface{}) Request

Query 设置请求代理

func Timeout

func Timeout(timeout time.Duration) Request

Timeout 设置请求代理

func Type

func Type(name string) Request

Type 请求提交方式,默认json

func UseRequest

func UseRequest(mdl requestMiddlewareHandler) Request

UseRequest 增加请求中间件

func UseResponse

func UseResponse(mdl responseMidlewareHandler) Request

UseResponse 增加响应中间件

func UserAgent

func UserAgent(name string) Request

UserAgent 设置请求 user-agent,默认是 chrome 75.0

func (Request) BaseURL

func (r Request) BaseURL(url string) Request

BaseURL 设置url前缀

func (Request) Clone

func (r Request) Clone() Request

func (Request) Cookie

func (r Request) Cookie(c *http.Cookie) Request

Cookie 设置请求 Cookie

func (Request) Del

func (r Request) Del(url string, body ...interface{}) (*Response, error)

Del 发起 delete 请求,body 是请求带的参数,可使用json字符串或者结构体

func (Request) Do

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

Do 发出请求,method 请求方法,url 请求地址, query 查询参数,body 请求数据,file 文件对象/地址

func (Request) Get

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

Get 发起 get 请求, query 查询参数

func (Request) Head

func (r Request) Head(url string, args ...interface{}) (*Response, error)

Head 发起 head 请求

func (Request) Header

func (r Request) Header(key, val string) Request

Header 设置请求 Header

func (Request) Options

func (r Request) Options(url string, args ...interface{}) (*Response, error)

Options 发起 options 请求,query 查询参数

func (Request) Patch

func (r Request) Patch(url string, body ...interface{}) (*Response, error)

Patch 发起 patch 请求,body 是请求带的参数,可使用json字符串或者结构体

func (Request) Post

func (r Request) Post(url string, body ...interface{}) (*Response, error)

Post 发起 post 请求,body 是请求带的参数,可使用json字符串或者结构体

func (Request) PostFile

func (r Request) PostFile(url string, file interface{}, body interface{}) (*Response, error)

PostFile 发起 post 请求上传文件,将使用表单提交,file 是文件地址或者文件流, body 是请求带的参数,可使用json字符串或者结构体

func (Request) Proxy

func (r Request) Proxy(url string) Request

Proxy 设置请求代理

func (Request) Put

func (r Request) Put(url string, body ...interface{}) (*Response, error)

Put 发起 put 请求,body 是请求带的参数,可使用json字符串或者结构体

func (Request) PutFile

func (r Request) PutFile(url string, file interface{}, body interface{}) (*Response, error)

PutFile 发起 put 请求上传文件,将使用表单提交,file 是文件地址或者文件流, body 是请求带的参数,可使用json字符串或者结构体

func (Request) Query

func (r Request) Query(query interface{}) Request

Query 增加查询参数

func (Request) Timeout

func (r Request) Timeout(timeout time.Duration) Request

Timeout 请求超时时间

func (Request) Type

func (r Request) Type(name string) Request

Type 请求提交方式,默认json

func (Request) UseRequest

func (r Request) UseRequest(mdl requestMiddlewareHandler) Request

UseRequest 增加请求中间件

func (Request) UseResponse

func (r Request) UseResponse(mdl responseMidlewareHandler) Request

UseResponse 增加响应中间件

func (Request) UserAgent

func (r Request) UserAgent(name string) Request

UserAgent 设置请求 user-agent,默认是 chrome 75.0

type Response

type Response struct {
	Request *Request
	Raw     *http.Response
	Body    []byte
	Errs    ResponseError
}

Response 回应对象

func Del

func Del(url string, body interface{}) (*Response, error)

Del 发起 delete 请求,body 是请求带的参数,可使用json字符串或者结构体

func Get

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

Get 发起 get 请求, query 查询参数

func Head(url string, query interface{}) (*Response, error)

Head 发起 head 请求

func NewResponse

func NewResponse() *Response

NewResponse 新建回应对象

func Options

func Options(url string, query interface{}) (*Response, error)

Options 发起 options 请求,query 查询参数

func Patch

func Patch(url string, body interface{}) (*Response, error)

Patch 发起 patch 请求,body 是请求带的参数,可使用json字符串或者结构体

func Post

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

Post 发起 post 请求,body 是请求带的参数,可使用json字符串或者结构体

func PostFile

func PostFile(url string, file interface{}, body interface{}) (*Response, error)

PostFile 发起 post 请求上传文件,将使用表单提交,file 是文件地址或者文件流, body 是请求带的参数,可使用json字符串或者结构体

func Put

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

Put 发起 put 请求,body 是请求带的参数,可使用json字符串或者结构体

func PutFile

func PutFile(url string, file interface{}, body interface{}) (*Response, error)

PutFile 发起 put 请求上传文件,将使用表单提交,file 是文件地址或者文件流, body 是请求带的参数,可使用json字符串或者结构体

func (*Response) Byte

func (r *Response) Byte() []byte

Byte 获取响应字节

func (*Response) Cookies

func (r *Response) Cookies() []*http.Cookie

Cookies 获取响应 cookie

func (*Response) Err

func (r *Response) Err() error

Err 获取响应错误

func (*Response) Header

func (r *Response) Header() http.Header

Header 获取响应header

func (*Response) IsError

func (r *Response) IsError() bool

IsError 是否响应错误

func (*Response) JSON

func (r *Response) JSON(v interface{}) error

JSON 根据json绑定结构体

func (*Response) Status

func (r *Response) Status() int

Status 获取响应状态码

func (*Response) String

func (r *Response) String() string

String 获取响应字符串

type ResponseError

type ResponseError []error

ResponseError 响应错误对象

func (ResponseError) Add

func (e ResponseError) Add(err error) ResponseError

Add 增加错误

func (ResponseError) Error

func (e ResponseError) Error() string

Error 实现 error 接口

func (ResponseError) HasErr

func (e ResponseError) HasErr() bool

HasErr 是否有错误

type SuperAgent added in v1.0.13

type SuperAgent struct {
	Url        string
	Method     string
	Header     http.Header
	TargetType string
	ForceType  string
	Data       map[string]interface{}
	SliceData  []interface{}
	FormData   url.Values
	QueryData  url.Values
	//FileData             []File
	BounceToRawString bool
	RawString         string
	Client            *http.Client
	Transport         *http.Transport
	Cookies           []*http.Cookie
	Errors            []error
	BasicAuth         struct{ Username, Password string }
	Debug             bool
	CurlCommand       bool
	//logger               Logger
	//Retryable            superAgentRetryable
	DoNotClearSuperAgent bool
	// contains filtered or unexported fields
}

func (*SuperAgent) Proxy added in v1.0.13

func (s *SuperAgent) Proxy(proxyUrl string) *SuperAgent

func (*SuperAgent) Query added in v1.0.13

func (s *SuperAgent) Query(content interface{}) *SuperAgent

func (*SuperAgent) Send added in v1.0.13

func (s *SuperAgent) Send(content interface{}) *SuperAgent

func (*SuperAgent) SendMap added in v1.0.13

func (s *SuperAgent) SendMap(content interface{}) *SuperAgent

func (*SuperAgent) SendSlice added in v1.0.13

func (s *SuperAgent) SendSlice(content []interface{}) *SuperAgent

func (*SuperAgent) SendString added in v1.0.13

func (s *SuperAgent) SendString(content string) *SuperAgent

func (*SuperAgent) SendStruct added in v1.0.13

func (s *SuperAgent) SendStruct(content interface{}) *SuperAgent

func (*SuperAgent) Timeout added in v1.0.13

func (s *SuperAgent) Timeout(timeout time.Duration) *SuperAgent

func (*SuperAgent) Type added in v1.0.13

func (s *SuperAgent) Type(typeStr string) *SuperAgent

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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