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

将用户名和邮箱作为请求包体

2024-03-31 Web开发

通过前面的学习,我们知道措置惩罚惩罚器函数需要切合下面的签名:

func (w http.ResponseWriter, r *http.Request)

此中,http.Request就是请求的类型。客户端通报的数据都可以通过这个布局来获取。布局Request界说在包 net/http 中:

// src/net/http/request.go type Request struct { Method string URL *url.URL Proto string ProtoMajor int ProtoMinor int Header Header Body io.ReadCloser ContentLength int // 省略一些字段... }

我们来看一下几个重要的字段。

Method

请求中的Method字段暗示客户端想要挪用处事器的哪个要领。在第一篇文章中,我们提到过 HTTP 协议要领。其取值有GET/POST/PUT/DELETE等。处事器按照请求要领的差别会进行差此外措置惩罚惩罚,,例如GET要领只是获取信息(用户根基信息,商品信息等),POST要领创建新的资源(注册新用户,上架新商品等)。

URL

Tim Berners-Lee 在创建万维网的同时,也引入了使用字符串来暗示互联网资源的观点。他称该字符串为统一资源标识符(URI,Uniform Resource Identifier)。URI 由两部分构成。一部分暗示资源的名称,即统一资源名称(URN,Uniform Resource Name)。另一部分暗示资源的位置,即统一资源定位符(URL,Uniform Resource Location)。

在 HTTP 请求中,使用 URL 来对要操纵的资源位置进行描述。URL 的一般格局为:

[scheme:][//[[email protected]]host][/]path[?query][#fragment]

scheme:协议名,常见的有httphttpsftp;

userInfo:若有,则暗示用户信息,如用户名和暗码可写作dj:password;

host:暗示主机域名或地点,和一个可选的端口信息。若端口未指定,则默认为 80。例如,:8080,127.0.0.1:8080;

path:资源在主机上的路径,以/分隔断绝分手,如/posts;

query:可选的盘问字符串,客户端传输过来的键值对参数,键值直接用=,多个键值对之间用&连接,如page=1&count=10;

fragment:片段,又叫锚点。暗示一个页面中的位置信息。由浏览器倡议的请求 URL 中,凡是没有这部分信息。但是可以通过ajax等代码的方法发送这个数据;

我们来看一个完整的 URL:

:[email protected]/posts?page=1&count=10#fmt

Go 中的 URL 布局界说在net/url包中:

// net/url/url.go type URL struct { Scheme string Opaque string User *Userinfo Host string Path string RawPath string RawQuery string Fragment string }

可以通过请求东西中的URL字段获取这些信息。接下来,我们编写一个措施来具体看看(使用上一篇文章讲的 Web 措施根基布局,只需要增加措置惩罚惩罚器函数和注册即可):

func urlHandler(w http.ResponseWriter, r *http.Request) { URL := r.URL fmt.Fprintf(w, "Scheme: %s\n", URL.Scheme) fmt.Fprintf(w, "Host: %s\n", URL.Host) fmt.Fprintf(w, "Path: %s\n", URL.Path) fmt.Fprintf(w, "RawPath: %s\n", URL.RawPath) fmt.Fprintf(w, "RawQuery: %s\n", URL.RawQuery) fmt.Fprintf(w, "Fragment: %s\n", URL.Fragment) } // 注册 mux.HandleFunc("/url", urlHandler)

运行处事器,通过浏览器访谒localhost:8080/url/posts?page=1&count=10#main:

Scheme: Host: Path: /url/posts RawPath: RawQuery: page=1&count=10 Fragment:

为什么会呈现空字段?注意到源码Request布局中URL字段上有一段注释:

// URL specifies either the URI being requested (for server // requests) or the URL to access (for client requests). // // For server requests, the URL is parsed from the URI // supplied on the Request-Line as stored in RequestURI. For // most requests, fields other than Path and RawQuery will be // empty. (See RFC 7230, Section 5.3) // // For client requests, the URL's Host specifies the server to // connect to, while the Request's Host field optionally // specifies the Host header value to send in the HTTP // request.

大意是作为处事器收到的请求时,URL中除了Path和RawQuery,其它字段大多为空。对付这个问题,Go 的 Github 货仓上Issue 28940有过讨论。

我们还可以通过URL布局得到一个 URL 字符串:

URL := &net.URL { Scheme: "http", Host: "example.com", Path: "/posts", RawQuery: "page=1&count=10", Fragment: "main", } fmt.Println(URL.String())

上面措施运行输出字符串:

?page=1&count=10#main Proto/ProtoMajor/ProtoMinor

Proto暗示 HTTP 协议版本,如HTTP/1.1,ProtoMajor暗示大版本,ProtoMinor暗示小版本。

func protoFunc(w http.ResponseWriter, r *http.Request) { fmt.Fprintf(w, "Proto: %s\n", r.Proto) fmt.Fprintf(w, "ProtoMajor: %d\n", r.ProtoMajor) fmt.Fprintf(w, "ProtoMinor: %d\n", r.ProtoMinor) } mux.HandleFunc("/proto", protoFunc)

启动处事器,浏览器请求localhost:8080返回:

Proto: HTTP/1.1 ProtoMajor: 1 ProtoMinor: 1

当前 HTTP/1.1 是主流的版本。

Header

Header中存放的客户端发送过来的首部信息,键-值对的形式。Header类型底层其实是map[string][]string:

// src/net/http/header.go type Header map[string][]string

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