This is a creation in
Article, where the information may have evolved or changed.
Online information is a bit miscellaneous, and some also doped middleware into, resulting in the use of obstacles, so, directly on the official documents:
1, first understand []byte string conversion, the simplest way is
String goto []byte:[]byte (para string)
[]byte Turn string:string ([]byte)
Then there are some implementations of IO packages, such as:
bytes.NewBuffer(para []byte).String()
2, how to get form,body,head from request, here is the official API:
type requestfunc newrequest (method, urlstr string, body io. Reader) (*request, error) func readrequest (b *bufio. Reader) (req *request, err Error) func (R *request) Protoatleast (major, Minor int) Boolfunc (R *request) useragent () string Func (R *request) Referer () Stringfunc (R *request) Addcookie (c *cookie) func (R *request) Setbasicauth (username, password String) func (R *request) Write (w io. Writer) Errorfunc (R *request) writeproxy (w io). Writer) Errorfunc (R *request) cookies () []*cookiefunc (R *request) cookie (name string) (*cookie, Error) func (R *request) Parseform () Errorfunc (R *request) parsemultipartform (maxmemory Int64) Errorfunc (R *request) Formvalue (key string) Strin Gfunc (R *request) Postformvalue (key string) Stringfunc (R *request) formfile (key string) (multipart. File, *multipart. Fileheader, Error) func (R *request) Multipartreader () (*multipart. Reader, error)
type Headerfunc (h Header) Get(key string) stringfunc (h Header) Set(key, value string)func (h Header) Add(key, value string)func (h Header) Del(key string)func (h Header) Write(w io.Writer) errorfunc (h Header) WriteSubset(w io.Writer, exclude map[string]bool) error
Where the type of body is the type of Io.read,head is map[string][]string, so the acquisition method is not the same:
Get Body:io.ioutil.ReadAll (req. Body), return []byte type of data, generally used for JSON
Gets the header:req. Header.get ("Para")
Gets the data in the form: req. Parseform, then, Para=postformvalue ("Para")
3, return the data to the request:
The data structure of response is relatively simple:
type Responsefunc ReadResponse(r *bufio.Reader, req *Request) (*Response, error)func (r *Response) ProtoAtLeast(major, minor int) boolfunc (r *Response) Cookies() []*Cookiefunc (r *Response) Location() (*url.URL, error)func (r *Response) Write([]byte) error//不同版本这个有差异
With write, the data is converted to byte.