简体   繁体   English

Golang的“ net / http”中的客户端HTTP请求和服务器HTTP请求有什么区别

[英]What is the difference between client HTTP request and server HTTP request in Golang's “net/http”

I have seen people use NewRequest() method of the "net/http" package for testing APIs. 我见过人们使用“ net / http”包的NewRequest()方法来测试API。 Why not use the NewRequest() method from "net/http/httptesting"? 为什么不使用“ net / http / httptesting”中的NewRequest()方法? What's the difference? 有什么不同? Documentation advises the following : 文档建议以下内容

// To generate a client HTTP request instead of a server request, see
// the NewRequest function in the net/http package.

What would be the difference in handling cookies, for example? 例如,处理Cookie有什么区别? Both seem to be very similar. 两者似乎非常相似。

TL;DR: they're the same type, used a bit differently for two use cases and initialized differently to serve these use cases TL; DR:它们是同一类型,在两个用例中使用的方式略有不同,并且在初始化时为使用这些用例的方式有所不同


The difference is only in usage - they are the same type http.Request . 区别仅在于用法-它们是相同的http.Request类型。 http.NewRequest is used for the more "production" use case which is client - "create a new request to send to the server". http.NewRequest用于客户端的更多“生产”用例-“创建要发送到服务器的新请求”。 When writing HTTP servers, it's occasionally useful to create requests for testing, which is what httptest.NewRequest does. 编写HTTP服务器时,偶尔创建测试请求很有用,这就是httptest.NewRequest所做的。 The doc of http.NewRequest is helpful here: http.NewRequest的文档在这里很有帮助:

NewRequest returns a Request suitable for use with Client.Do or Transport.RoundTrip. NewRequest返回适合与Client.Do或Transport.RoundTrip一起使用的请求。 To create a request for use with testing a Server Handler, either use the NewRequest function in the net/http/httptest package, use ReadRequest, or manually update the Request fields. 若要创建用于测试服务器处理程序的请求,请使用net / http / httptest程序包中的NewRequest函数,使用ReadRequest,或手动更新Request字段。 See the Request type's documentation for the difference between inbound and outbound request fields. 有关入站和出站请求字段之间的区别,请参见请求类型的文档。

If you check the docs of the http.Request type , you'll find things like: 如果查看http.Request类型的文档,则会发现类似以下内容:

// 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 *url.URL

Note the "For client requests" vs. "For server requests". 注意“对于客户端请求”与“对于服务器请求”。

If you see a place that doesn't use httptest.NewRequest it could be because: 如果您看到一个不使用httptest.NewRequest的地方,可能是因为:

  1. They're not aware of it 他们不知道
  2. Or they need more careful fine-tuning that http.NewRequest doesn't provide 或者他们需要更仔细的微调,而http.NewRequest没有提供

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM