当前位置:首页 > Web开发 > 正文

Golang Http请求

2024-03-31 Web开发

请求的布局
HTTP的交互以请求和响应的应答模式。go的请求我们早就见过了,handler函数的第二个参数http.Requests。其布局为:

type Request struct { Method string URL *url.URL Proto string // "HTTP/1.0" ProtoMajor int // 1 ProtoMinor int // 0 Header Header Body io.ReadCloser ContentLength int64 TransferEncoding []string Close bool Host string Form url.Values PostForm url.Values MultipartForm *multipart.Form .... ctx context.Context }

从request布局可以看到,http请求的根基信息都囊括了。对付请求而言,主要存眷一下请求的URL,Method,Header,Body这些布局。

URL
HTTP的url请求格局为scheme://[[email protected]]host/path[?query][#fragment], go的供给了一个URL布局,用来映射HTTP的请求URL。

type URL struct { Scheme string Opaque string User *Userinfo Host string Path string RawQuery string Fragment string }

URL的格局对照明确,其实更好的名词应该是URI,统一资源定位。url中对照重要的是盘问字符串query。凡是作为get请求的参数。query是一些使用&标记支解的key1=value1&key2=value2键值对,由于url编码是ASSIC码,因此query需要进行urlencode。go可以通过request.URI.RawQuery读取query

func indexHandler(w http.ResponseWriter, r *http.Request) { info := fmt.Sprintln("URL", r.URL, "HOST", r.Host, "Method", r.Method, "RequestURL", r.RequestURI, "RawQuery", r.URL.RawQuery) fmt.Fprintln(w, info) } ? ~ curl -X POST -H "Content-Type: application/x-www-form-urlencoded" -d 'name=vanyar&age=27' ":8000?lang=zh&version=1.1.0" URL /?lang=zh&version=1.1.0 HOST 127.0.0.1:8000 Method POST RequestURL /?lang=zh&version=1.1.0 RawQuery lang=zh&version=1.1.0

header
header也是HTTP中重要的构成部分。Request布局中就有Header布局,Header素质上是一个map(map[string][]string)。将http协议的header的key-value进行映射成一个图:

Host: example.com accept-encoding: gzip, deflate Accept-Language: en-us fOO: Bar foo: two Header = map[string][]string{ "Accept-Encoding": {"gzip, deflate"}, "Accept-Language": {"en-us"}, "Foo": {"Bar", "two"}, }

header中的字段包罗了很多通信的设置,很多时候请求都需要指定Content-Type。

func indexHandler(w http.ResponseWriter, r *http.Request) { info := fmt.Sprintln(r.Header.Get("Content-Type")) fmt.Fprintln(w, info) } ? ~ curl -X POST -H "Content-Type: application/x-www-form-urlencoded" -d 'name=vanyar&age=27' ":8000?lang=zh&version=1.1.0"

application/x-www-form-urlencoded
Golng 供给了不少打印函数,根基上分为三类三种。即 Print Println 和Printf。
Print对照简单,打印输出到标准输出流,Println则也一样差别在于多打印一个换行符。至于Printf则是打印格局化字符串,三个要领都返回打印的bytes数。Sprint,Sprinln和Sprintf则返回打印的字符串,不会输出到标准流中。Fprint,Fprintf和Fprinln则把输出的功效打印输出到io.Writer接口中,http中则是http.ReponseWriter这个东西中,返回打印的bytes数。

Body
http中数据通信,主要通过body传输。go把body封装成Request的Body,它是一个ReadCloser接口。接口要领Reader也是一个接口,后者有一个Read(p []byte) (n int, err error)要领,因此body可以通过读取byte数组获取请求的数据。

func indexHandler(w http.ResponseWriter, r *http.Request) { info := fmt.Sprintln(r.Header.Get("Content-Type")) len := r.ContentLength body := make([]byte, len) r.Body.Read(body) fmt.Fprintln(w, info, string(body)) } ? ~ curl -X POST -H "Content-Type: application/x-www-form-urlencoded" -d 'name=vanyar&age=27' ":8000?lang=zh&version=1.1.0" application/x-www-form-urlencoded name=vanyar&age=27 可见,当请求的content-type为application/x-www-form-urlencoded, body也是和query一样的格局,key-value的键值对。换成json的请求方法则如下: ? ~ curl -X POST -H "Content-Type: application/json" -d '{name: "vanyar", age: 27}' ":8000?lang=zh&version=1.1.0" application/json {name: "vanyar", age: 27} multipart/form-data的格局用来上传图片,请求的body如下: ? ~ curl -X POST -H "Content-Type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW" -F "name=vanyar" -F "age=27" ":8000?lang=zh&version=1.1.0" multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW; boundary=------------------------d07972c7800e4c23 --------------------------d07972c7800e4c23 Content-Disposition: form-data; name="name" vanyar --------------------------d07972c7800e4c23 Content-Disposition: form-data; name="age" 27 --------------------------d07972c7800e4c23--

温馨提示: 本文由Jm博客推荐,转载请保留链接: https://www.jmwww.net/file/web/29895.html