Request¶
让我们以Request's readme中的第一个例子为例:
对于 Got,它是:
现在看起来好多了,是吧? 😎
常见的选项¶
这些 Got 选项与 Request 相同:
urlbodyfollowRedirectencodingmaxRedirectslocalAddressheaderscreateConnection- UNIX sockets:
http://unix:SOCKET:PATH
time 选项不存在,假设它总是正确.
如果你熟悉这些,就可以开始了。
重命名选项¶
Note
得到存储HTTPS选项在httpoptions。其中一些已经被重新命名。
了解更多.
可读性对我们来说非常重要,所以我们对这些选项有不同的名称:
qs→searchParamsstrictSSL→rejectUnauthorizedgzip→decompressjar→cookieJar(acceptstough-cookiejar)jsonReviver→parseJsonjsonReplacer→stringifyJson
行为变化¶
- The
agentoption is now an object withhttp,httpsandhttp2properties. - The
timeoutoption is now an object. You can set timeouts on particular events! - The
searchParamsoption is always serialized usingURLSearchParams. - In order to pass a custom query string, provide it with the
urloption.
got('https://example.com', {searchParams: {test: ''}})→https://example.com/?test=
got('https://example.com/?test')→https://example.com/?test - To use streams, call
got.stream(url, options)orgot(url, {…, isStream: true}).
突发的变化¶
- The
jsonoption is not aboolean, it's anobject. It will be stringified and used as a body. - The
formoption is anobjectand will be used asapplication/x-www-form-urlencodedbody. - All headers are converted to lowercase.
According to the spec, the headers are case-insensitive. - No
oauth/hawk/aws/httpSignatureoption.
To sign requests, you need to create a custom instance. - No
agentClass/agentOptions/pooloption. - No
foreveroption.
You need to pass an agent withkeepAliveoption set totrue. - No
proxyoption. You need to pass a custom agent. - No
authoption.
You need to useusername/passwordinstead or set theauthorizationheader manually. - No
baseUrloption.
Instead, there isprefixUrlwhich appends a trailing slash if not present. - No
removeRefererHeaderoption.
You can remove therefererheader in abeforeRequesthook. - No
followAllRedirectsoption.
Hooks are very powerful. Read more to see what else you achieve using hooks.
关于流的更多信息¶
让我们快速看一下 Request 自述中的另一个例子:
这里很酷的特性是 Request 可以用流代理报头,但 Got 也可以这样做!
就流而言,什么都没有真正改变。
方便的方法¶
- If you were using
request.get,request.post, and so on - you can do the same with Got. - The
request.defaults({…})method has been renamed. You can do the same withgot.extend({…}). - There is no
request.cookie()norrequest.jar(). You have to usetough-cookiedirectly.
你可以开始了!¶
好吧,你已经走了这么远
看一下文档。值得花时间读一读。
这里有一些很棒的建议。
如果某件事不清楚或没有按照它应该的方式运行,不要犹豫打开一个问题.