request总的来说就是 接受一个来自服务端或者客户端的http请求
看看request的结构体定义
type Request struct {
Method string //方法可以是 (GET, POST, PUT, etc.).
//客户端请求一个空的string,那他就是一个get
URL *url.URL // 指定被请求的URI(服务端请求)或URL来访问(客户机请求)。
//对客户端请求来说 ,就是记录的要连接的服务端的地址
Proto string // "HTTP/1.0"
ProtoMajor int // 1
ProtoMinor int // 0
// Header contains the request header fields either received
// by the server or to be sent by the client.
//
// If a server received a request with header lines,
//
// Host: example.com
// accept-encoding: gzip, deflate
// Accept-Language: en-us
// fOO: Ba
// foo: two
//
// then
//
// Header = map[string][]string{
// "Accept-Encoding": {"gzip, deflate"},
// "Accept-Language": {"en-us"},
// "Foo": {"Bar", "two"},
// }
//
Header Heade
Body io.ReadCloser // request的body信息部分 空的request表示没有body ,get 请求
//之后 http的发起段client要调用 close方法 结束这次request
//由于服务端通常都是处理的request body部分是非空的 ,所以会反馈一个EOF ,服务端会直接关闭这个
//request ,不会做任何实质性的处理
ContentLength int64 // request body的大小 ,bytes
//一般 >=0 , -1 表示 unknown
// 如果 =0 ,body 不为空,也表示 unknown
TransferEncoding []string //TransferEncoding 列举了 transfer 的编码 从最外层到最内层
// 空值表示 "identity" encoding.
// chunked encoding 是可以自动添加和删除的
Close bool // 当回复了request之后结束这次链接
// 对服务端来说就是回复了这个 request ,对客户端来说就是收到了 response
//对服务端 Handlers 会自动调用关闭 close
//对客户端 如果设置了tcp的长连接 Transport.DisableKeepAlives=false ,那么不会关闭
// Transport.DisableKeepAlives=true 不保持长连接 就 close掉
// For server requests Host specifies the host on which the
// URL is sought. Per RFC 2616, this is either the value of
// the "Host" header or the host name given in the URL itself.
// It may be of the form "host:port".
//
// For client requests Host optionally overrides the Host
// header to send. If empty, the Request.Write method uses
// the value of URL.Host.
Host string //服务器请求指定的主机
// host 头信息或者名字 是已url的方式给出 或者是 "host:port".
//客户端请求主机选择覆盖主机头发送。如果空,使用URL.Host的价值。
// Form contains the parsed form data, including both the URL
// field's query parameters and the POST or PUT form data.
// This field is only available after ParseForm is called.
// The HTTP client ignores Form and uses Body instead.
Form url.Values
// PostForm contains the parsed form data from POST, PATCH,
// or PUT body parameters.
//
// This field is only available after ParseForm is called.
// The HTTP client ignores PostForm and uses Body instead.
PostForm url.Values
// MultipartForm is the parsed multipart form, including file uploads.
// This field is only available after ParseMultipartForm is called.
// The HTTP client ignores MultipartForm and uses Body instead.
MultipartForm *multipart.Form
// Trailer specifies additional headers that are sent after the request
// body.
//
// For server requests the Trailer map initially contains only the
// trailer keys, with nil values. (The client declares which trailers it
// will later send.) While the handler is reading from Body, it must
// not reference Trailer. After reading from Body returns EOF, Traile
// can be read again and will contain non-nil values, if they were sent
// by the client.
//
// For client requests Trailer must be initialized to a map containing
// the trailer keys to later send. The values may be nil or their final
// values. The ContentLength must be 0 or -1, to send a chunked request.
// After the HTTP request is sent the map values can be updated while
// the request body is read. Once the body returns EOF, the caller must
// not mutate Trailer.
//
// Few HTTP clients, servers, or proxies support HTTP trailers.
Trailer Header // 当body部分发送完成之后 Trailer 才会添加到头信息中
RemoteAddr string //服务端会记录发送request的服务端IP
//当调用一个处理程序之前 httpserver会设置 该字段RemoteAddr 格式,IP:port
//客户端没有该字段
// RequestURI is the unmodified Request-URI of the
// Request-Line (RFC 2616, Section 5.1) as sent by the client
// to a server. Usually the URL field should be used instead.
// It is an error to set this field in an HTTP client request.
RequestURI string
// TLS allows HTTP servers and other software to record
// information about the TLS connection on which the request
// was received. This field is not filled in by ReadRequest.
// The HTTP server in this package sets the field fo
// TLS-enabled connections before invoking a handler;
// otherwise it leaves the field nil.
// This field is ignored by the HTTP client.
TLS *tls.ConnectionState
// Cancel is an optional channel whose closure indicates that the client
// request should be regarded as canceled. Not all implementations of
// RoundTripper may support Cancel.
//
// For server requests, this field is not applicable.
//
// Deprecated: Use the Context and WithContext methods
// instead. If a Request's Cancel field and context are both
// set, it is undefined whether Cancel is respected.
Cancel <-chan struct{}
Response *Response
// 这个字段只有在重定向的时候才会被调用 ,也只使用在客户端的重定向
方法调用
func NewRequest(method, urlStr string, body io.Reader) (*Request, error)
func ReadRequest(b *bufio.Reader) (*Request, error)
func (r *Request) AddCookie(c *Cookie)
func (r *Request) BasicAuth() (username, password string, ok bool)
func (r *Request) Context() context.Context
func (r *Request) Cookie(name string) (*Cookie, error)
func (r *Request) Cookies() []*Cookie
func (r *Request) FormFile(key string) (multipart.File, *multipart.FileHeader, error)
func (r *Request) FormValue(key string) string
func (r *Request) MultipartReader() (*multipart.Reader, error)
func (r *Request) ParseForm() erro
func (r *Request) ParseMultipartForm(maxMemory int64) erro
func (r *Request) PostFormValue(key string) string
func (r *Request) ProtoAtLeast(major, minor int) bool
func (r *Request) Referer() string
func (r *Request) SetBasicAuth(username, password string)
func (r *Request) UserAgent() string
func (r *Request) WithContext(ctx context.Context) *Request
func (r *Request) Write(w io.Writer) erro
func (r *Request) WriteProxy(w io.Writer) erro
//看一个简单的获取UserAgent头信息的方法 ,其他的类似
func (r *Request) UserAgent() string {
return r.Header.Get("User-Agent")
}
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。