httputil

package
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Aug 15, 2024 License: Apache-2.0 Imports: 12 Imported by: 0

Documentation

Index

Constants

View Source
const (
	UserHeader                 = "User-ID"
	OrgHeader                  = "Org-ID"
	InternalHeader             = "Internal-Client"        // 内部服务间调用时使用
	InternalActionHeader       = "Internal-Action-Client" // action calls the api header
	RequestIDHeader            = "RequestID"
	UserInfoDesensitizedHeader = "Openapi-Userinfo-Desensitized"
	LangHeader                 = "lang"
	UseTokenHeader             = "Use-Token"
	ContentTypeHeader          = "Content-Type"
	AuthorizationHeader        = "Authorization"

	ClientIDHeader           = "Client-ID"
	ClientNameHeader         = "Client-Name"
	CookieNameOpenapiSession = "OPENAPISESSION"
)

dice 公共 Header

Variables

View Source
var (
	HeaderKeyContentType              = textproto.CanonicalMIMEHeaderKey("content-type")
	HeaderKeyContentLength            = textproto.CanonicalMIMEHeaderKey("content-length")
	HeaderKeyAccessControlAllowOrigin = textproto.CanonicalMIMEHeaderKey("Access-Control-Allow-Origin")
	HeaderKeyAuthorization            = textproto.CanonicalMIMEHeaderKey("Authorization")
	HeaderKeyAccept                   = textproto.CanonicalMIMEHeaderKey("Accept")
	HeaderKeyAcceptEncoding           = textproto.CanonicalMIMEHeaderKey("Accept-Encoding")
	HeaderKeyUserAgent                = textproto.CanonicalMIMEHeaderKey("User-Agent")
	HeaderKeyContentDisposition       = textproto.CanonicalMIMEHeaderKey("Content-Disposition")
	HeaderKeyContentEncoding          = textproto.CanonicalMIMEHeaderKey("Content-Encoding")
)
View Source
var (
	ContentTypeMultiPartFormData = "multipart/form-data"
)

Functions

func CopyHeader

func CopyHeader[R RequestResponse](r R) http.Header

func DeepCopyRequest

func DeepCopyRequest(r *http.Request) (*http.Request, error)

func DeepCopyRequestResponse

func DeepCopyRequestResponse[R RequestResponse](r R) (R, error)

func DeepCopyResponse

func DeepCopyResponse(response *http.Response) (*http.Response, error)

func GetUserAgent

func GetUserAgent(header http.Header) string

func HeaderContains

func HeaderContains[Header http.Header | []String | []string | ~string, String ~string](header Header, s String) bool

func JoinPath

func JoinPath(appendRoot bool, segments ...string) string

JoinPath .

func JoinPathR

func JoinPathR(segments ...string) string

JoinPathR .

func NopCloseReadBody

func NopCloseReadBody[R RequestResponse](r R) (*bytes.Buffer, error)

NopCloseReadBody is equivalent to the following two functions

func NopCloseReadRequestBody(r *http.Request) ([]byte, error) {
	if r == nil || r.Body == nil {
		return nil, nil
	}
	var buf bytes.Buffer
	if _, err := buf.ReadFrom(r.Body); err != nil {
		return nil, err
	}
	if err := r.Body.Close(); err != nil {
		return nil, err
	}
	r.Body = io.NopCloser(&buf)
	return buf.Bytes(), nil
}

func NopCloseReadResponseBody(r *http.Response) ([]byte, error) {
	if r == nil || r.Body == nil {
		return nil, nil
	}
	var buf bytes.Buffer
	if _, err := buf.ReadFrom(r.Body); err != nil {
		return nil, err
	}
	if err := r.Body.Close(); err != nil {
		return nil, err
	}
	r.Body = io.NopCloser(&buf)
	return buf.Bytes(), nil
}

func NopCloseReadBodyBuf

func NopCloseReadBodyBuf[R RequestResponse](buf *bytes.Buffer, r R) (int64, error)

Types

type ContentType

type ContentType string
const (
	ApplicationJson     ContentType = "application/json"
	ApplicationJsonUTF8 ContentType = "application/json; charset=utf-8"
	TextEventStream     ContentType = "text/event-stream"
	URLEncodedFormMime  ContentType = "application/x-www-form-urlencoded"
)

type RequestResponse

type RequestResponse interface {
	http.Request | ~*http.Request | http.Response | ~*http.Response
}

Jump to

Keyboard shortcuts

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