http

package
v1.5.5 Latest Latest
Warning

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

Go to latest
Published: Dec 4, 2024 License: Apache-2.0 Imports: 13 Imported by: 3

README

http

http包是简化封装解析,在公司这边有标准的返回结构,但是我们很多时候只使用其中的data部分,很多时候自己需要判断并解析,这里对这个做了简单的工具封装

{
  "code": 0,
  "message": "yy",
  "data": "xx"
}

示例

func TestGetSimple(t *testing.T) {
    // {"code":"success","data":"ok","message":"成功"}
    data, err := http.GetSimple("http://localhost:8082/api/api/app/sample/test/get")
    if err != nil {
        fmt.Errorf(err.Error())
        return
    }
    fmt.Println(string(data))

    // "ok"
    data, err = http.GetSimpleOfStandard("http://localhost:8082/api/api/app/sample/test/get")
    if err != nil {
        fmt.Errorf(err.Error())
        return
    }
    fmt.Println(string(data))
}

配置

# http的配置
base:
  http:
    # 通用的超时配置,链接、重定向、响应的所有超时时间;默认0,就是不超时
    timeout: 5s
    transport:
      # 握手的最长时间
      tls-handshake-timeout: 10s
      # 禁用HTTP keep alives,且将仅对单个HTTP请求使用到服务器的连接。 这与类似命名的TCP keep-alives无关
      disable-keep-alives: true
      # 禁用gzip的压缩标示
      disable-compression: true
      # 最大空闲连接数
      max-idle-conns: 100
      # 每个主机最大空闲连接数
      max-idle-conns-per-host: 100
      # 每个主机最大连接数
      max-conns-per-host: 100
      # 连接在关闭之前保持空闲的最长时间
      idle-conn-timeout: 90s
      # 完全写入请求后等待服务器响应标头的时间
      response-header-timeout: 15s
      # 在请求具有“Expect:100 continue”标头时,在完全写入请求标头后等待服务器的第一个响应标头的时间
      expect-continue-timeout: 1s
      # 指定服务器响应标头中允许的响应字节数限制
      max-response-header-bytes: 1024
      # 写入缓冲区的大小;如果为零,则使用默认值(当前为4KB)
      write-buffer-size: 4096
      # 从传输读取时使用的读取缓冲区的大小。如果为零,则使用默认值(当前为4KB)
      read-buffer-size: 4096
      # 使用Dial、DialTLS或DialContext func或TLSClientConfig字段时候,默认关闭http2;如果想要开启,则请设置为true
      force-attempt-HTTP2: true
      # 用于创建未加密TCP连接
      dial-context:
        # 超时是拨号等待连接完成的最长时间。如果同时设置了Deadline,则可能会更早失败。 默认值为无超时。
        timeout: 30s
        # 超时的绝对时间
        # deadline: "2023-02-13"
        # 活动网络连接的保持活动探测之间的间隔;默认15s
        keep-alive: 30s

如下是http的默认配置 Transport DefaultTransport

base:
  http:
    transport:
      # 使用Dial、DialTLS或DialContext func或TLSClientConfig字段时候,默认关闭http2;如果想要开启,则请设置为true
      force-attempt-HTTP2: true
      # 最大空闲连接数
      max-idle-conns: 100
      # 连接在关闭之前保持空闲的最长时间
      idle-conn-timeout: 90s
      # 握手的最长时间
      tls-handshake-timeout: 10s
      # 在请求具有“Expect:100 continue”标头时,在完全写入请求标头后等待服务器的第一个响应标头的时间
      expect-continue-timeout: 1s
      # 用于创建未加密TCP连接
      dial-context:
        # 超时是拨号等待连接完成的最长时间。如果同时设置了Deadline,则可能会更早失败。 默认值为无超时。
        timeout: 30s
        # 活动网络连接的保持活动探测之间的间隔;默认15s
        keep-alive: 30s

Documentation

Index

Constants

View Source
const (
	MaxIdleConns          int    = 100
	MaxIdleConnsPerHost   int    = 100
	IdleConnTimeout       int    = 90
	ContentTypeJson       string = "application/json; charset=utf-8"
	ContentTypeHtml       string = "text/html; charset=utf-8"
	ContentTypeText       string = "text/plain; charset=utf-8"
	ContentTypeCss        string = "text/css; charset=utf-8"
	ContentTypeJavaScript string = "application/x-javascript; charset=utf-8"
	ContentTypeJpeg       string = "image/jpeg"
	ContentTypePng        string = "image/png"
	ContentTypeGif        string = "image/gif"
	ContentTypeAll        string = "*/*"
	ContentPostForm       string = "application/x-www-form-urlencoded"
)

Variables

View Source
var NetHttpHooks []GobaseHttpHook

Functions

func AddHook added in v1.4.5

func AddHook(httpHook GobaseHttpHook)

func Delete

func Delete(url string, header http.Header, parameterMap map[string]string) (int, http.Header, any, error)

func DeleteOfStandard

func DeleteOfStandard(url string, header http.Header, parameterMap map[string]string) (int, http.Header, any, error)

func DeleteSimple

func DeleteSimple(url string) (int, http.Header, any, error)

func DeleteSimpleOfStandard

func DeleteSimpleOfStandard(url string) (int, http.Header, any, error)

func Do added in v1.4.12

func Do(httpRequest *http.Request) (int, http.Header, any, error)

func Get

func Get(url string, header http.Header, parameterMap map[string]string) (int, http.Header, any, error)

func GetClient added in v1.3.0

func GetClient() *http.Client

func GetOfStandard

func GetOfStandard(url string, header http.Header, parameterMap map[string]string) (int, http.Header, any, error)

func GetSimple

func GetSimple(url string) (int, http.Header, any, error)

func GetSimpleOfStandard

func GetSimpleOfStandard(url string) (int, http.Header, any, error)
func Head(url string, header http.Header, parameterMap map[string]string) error

func HeadSimple

func HeadSimple(url string) error

func Patch

func Patch(url string, header http.Header, parameterMap map[string]string, body any) (int, http.Header, any, error)

func PatchOfStandard

func PatchOfStandard(url string, header http.Header, parameterMap map[string]string, body any) (int, http.Header, any, error)

func PatchSimple

func PatchSimple(url string, body any) (int, http.Header, any, error)

func PatchSimpleOfStandard

func PatchSimpleOfStandard(url string, body any) (int, http.Header, any, error)

func Post

func Post(url string, header http.Header, parameterMap map[string]string, body any) (int, http.Header, any, error)

func PostForm added in v1.1.0

func PostForm(url string, header http.Header, parameterMap map[string]any) (int, http.Header, any, error)

func PostOfStandard

func PostOfStandard(url string, header http.Header, parameterMap map[string]string, body any) (int, http.Header, any, error)

func PostSimple

func PostSimple(url string, body any) (int, http.Header, any, error)

func PostSimpleOfStandard

func PostSimpleOfStandard(url string, body any) (int, http.Header, any, error)

func Put

func Put(url string, header http.Header, parameterMap map[string]string, body any) (int, http.Header, any, error)

func PutOfStandard

func PutOfStandard(url string, header http.Header, parameterMap map[string]string, body any) (int, http.Header, any, error)

func PutSimple

func PutSimple(url string, body any) (int, http.Header, any, error)

func PutSimpleOfStandard

func PutSimpleOfStandard(url string, body any) (int, http.Header, any, error)

func SetHttpClient

func SetHttpClient(httpClientOuter *http.Client)

func UrlWithParameter added in v1.5.3

func UrlWithParameter(url string, parameterMap map[string]string) string

Types

type DataResponse

type DataResponse[T any] struct {
	Code    int    `json:"code"`
	Message string `json:"message"`
	Data    T      `json:"data"`
}

type GobaseHttpHook added in v1.4.5

type GobaseHttpHook interface {
	Before(ctx context.Context, req *http.Request) (context.Context, http.Header)
	After(ctx context.Context, rsp *http.Response, rspCode int, rspData any, err error)
}

type NetError

type NetError struct {
	ErrMsg string
}

func (*NetError) Error

func (error *NetError) Error() string

Jump to

Keyboard shortcuts

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