httpKit

package
v1.3.10 Latest Latest
Warning

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

Go to latest
Published: Mar 21, 2023 License: Apache-2.0 Imports: 16 Imported by: 1

Documentation

Overview

Package httpKit

请求头 || 响应头 key: 不区分大小写

Index

Constants

View Source
const (
	// PlainContentType 纯文本格式
	PlainContentType = "text/plain; charset=utf-8"
	// JsonContentType JSON数据格式
	JsonContentType = "application/json; charset=utf-8"
	// OctetStreamContentType 二进制流数据(如常见的文件下载)
	/*
		参考:https://www.runoob.com/http/http-content-type.html
	*/
	OctetStreamContentType = "application/octet-stream; charset=utf-8"
)

Variables

This section is empty.

Functions

func AddHeader

func AddHeader(header http.Header, key, value string)

AddHeader

e.g.

header := make(map[string][]string)

fmt.Println(header)					// map[]
httpKit.AddHeader(header, "k", "0")
fmt.Println(header)					// map[K:[0]]
httpKit.AddHeader(header, "k", "1")
fmt.Println(header)					// map[K:[0 1]]

func ClearCookies

func ClearCookies(req *http.Request)

ClearCookies 清空Cookie.

func DelHeader

func DelHeader(header http.Header, key string)

func GetContentType

func GetContentType(data []byte) string

GetContentType 获取 ContentType(即MimeType).

@return 保底 "application/octet-stream"

e.g. ([]byte(nil)) => "text/plain; charset=utf-8" ([]byte{}) => "text/plain; charset=utf-8"

func GetFormFile

func GetFormFile(r *http.Request, key string, maxMemory int64) (multipart.File, *multipart.FileHeader, error)

GetFormFile

[Go] golang获取http中的get传递的参数

https://www.cnblogs.com/taoshihan/p/12943118.html

@param maxMemory 可以为-1,此时将采用默认值

func GetHeader

func GetHeader(header http.Header, key string) string

GetHeader

PS: 存在多个值的话([]string),返回第一个值.

@param key 不存在对应值的话,将返回 ""

func GetHeaderValues

func GetHeaderValues(header http.Header, key string) []string

GetHeaderValues

@param key 不存在对应值的话,将返回 nil

func GetOrigin

func GetOrigin(header http.Header) string

GetOrigin 获取请求的Origin

func GetPostParam

func GetPostParam(r *http.Request, key string) (string, error)

GetPostParam

[Go] golang获取http中的get传递的参数

https://www.cnblogs.com/taoshihan/p/12943118.html

func GetProto

func GetProto(req *http.Request) string

GetProto

@return "HTTP/1.0" || "HTTP/1.1" || ...

func GetRequestRoute

func GetRequestRoute(req *http.Request) string

GetRequestRoute 获取请求的路由.

func GetRequestUrl

func GetRequestUrl(req *http.Request) string

GetRequestUrl 返回当前接口的url.

PS: 不包括query数据.

func GetUrlParam

func GetUrlParam(r *http.Request, key string) string

GetUrlParam

[Go] golang获取http中的get传递的参数

https://www.cnblogs.com/taoshihan/p/12943118.html

func GetUrlParam1

func GetUrlParam1(r *http.Request, key string) (string, error)

GetUrlParam1

[Go] golang获取http中的get传递的参数

https://www.cnblogs.com/taoshihan/p/12943118.html

func GetUserAgent

func GetUserAgent(header http.Header) string

GetUserAgent 获取http请求头中"User Agent"的值.

参考: https://www.sunzhongwei.com/golang-gin-for-user-agent-in-http-request-header-value

e.g. Chrome浏览器: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36 Safari浏览器: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/15.5 Safari/605.1.15

func OverridePostRequestBody

func OverridePostRequestBody(req *http.Request, m map[string]string)

OverridePostRequestBody 覆盖POST请求的请求体.

func PolyfillContextPath

func PolyfillContextPath(relativePath string) string

PolyfillContextPath *

  • e.g.
  • "" => ""
  • "/" => "/"
  • "//" => "/"
  • "///" => "/"
  • "/c/////c//" => "/c/c/" *
  • @return 优化过的ContextPath

func RespondData

func RespondData(w http.ResponseWriter, code int, contentType string, data []byte) error

RespondData 响应字节流(二进制流)

参考: gin里面的 Context.Data() .

@return 如果不为nil,建议输出到控制台

func RespondFile

func RespondFile(w http.ResponseWriter, r *http.Request, code int, filePath, fileName string) error

RespondFile 响应文件

参考: gin里面的 Context.File() 和 Context.FileAttachment() .

@param filePath 文件路径 @param fileName 文件名(可以为"",此时将从 传参filePath 中获取) @return 如果不为nil,建议输出到控制台

func RespondJson

func RespondJson(w http.ResponseWriter, code int, obj any) error

RespondJson

参考: gin里面的 Context.JSON() .

func RespondString

func RespondString(w http.ResponseWriter, code int, format string, values ...any) error

RespondString

参考: gin里面的 Context.String() .

func RespondStringData

func RespondStringData(w http.ResponseWriter, code int, data []byte) error

func SetCacheControlNoCache

func SetCacheControlNoCache(header http.Header)

SetCacheControlNoCache 实际上是有缓存的)浏览器对请求回来的response做缓存,但是每次在向客户端(浏览器)提供响应数据时,缓存都要向服务器评估缓存响应的有效性。

PS: (1) 一般情况下, "no-cache" 就够了; (2) 详见"Web.docx".

func SetCacheControlNoStore

func SetCacheControlNoStore(header http.Header)

SetCacheControlNoStore 禁止一切缓存.

PS: 详见"Web.docx".

func SetHeader

func SetHeader(header http.Header, key, value string)

SetHeader

e.g.

header := make(map[string][]string)

fmt.Println(header) 				// map[]
httpKit.AddHeader(header, "k", "0")
fmt.Println(header) 				// map[K:[0]]
httpKit.AddHeader(header, "k", "1")
fmt.Println(header) 				// map[K:[0 1]]

httpKit.SetHeader(header, "k", "2")
fmt.Println(header) 				// map[K:[2]]
httpKit.SetHeader(header, "k", "")
fmt.Println(header) 				// map[]

func Status

func Status(w http.ResponseWriter, code int)

Status 设置响应的http状态码

PS: (1) 不建议多次设置 http状态码; (2) 如果多次设置的话,感觉 第一次设置的值 会生效.

@param code -1: 不设置http状态码

Types

type Repeat

type Repeat struct {
	Reader io.ReaderAt
	Offset int64
}

func (*Repeat) Close

func (p *Repeat) Close() error

func (*Repeat) Read

func (p *Repeat) Read(val []byte) (n int, err error)

Read 重写读方法,使每次读取request.Body时能从指定位置读取

func (*Repeat) Reset

func (p *Repeat) Reset()

Reset 重置偏移量

Jump to

Keyboard shortcuts

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