Request¶
让我们以Request's readme中的第一个例子为例:
对于 Got,它是:
现在看起来好多了,是吧? 😎
常见的选项¶
这些 Got 选项与 Request 相同:
url
body
followRedirect
encoding
maxRedirects
localAddress
headers
createConnection
- UNIX sockets:
http://unix:SOCKET:PATH
time
选项不存在,假设它总是正确.
如果你熟悉这些,就可以开始了。
重命名选项¶
Note
得到存储HTTPS选项在httpoptions
。其中一些已经被重新命名。
了解更多.
可读性对我们来说非常重要,所以我们对这些选项有不同的名称:
qs
→searchParams
strictSSL
→rejectUnauthorized
gzip
→decompress
jar
→cookieJar
(acceptstough-cookie
jar)jsonReviver
→parseJson
jsonReplacer
→stringifyJson
行为变化¶
- The
agent
option is now an object withhttp
,https
andhttp2
properties. - The
timeout
option is now an object. You can set timeouts on particular events! - The
searchParams
option is always serialized usingURLSearchParams
. - In order to pass a custom query string, provide it with the
url
option.
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
json
option is not aboolean
, it's anobject
. It will be stringified and used as a body. - The
form
option is anobject
and will be used asapplication/x-www-form-urlencoded
body. - All headers are converted to lowercase.
According to the spec, the headers are case-insensitive. - No
oauth
/hawk
/aws
/httpSignature
option.
To sign requests, you need to create a custom instance. - No
agentClass
/agentOptions
/pool
option. - No
forever
option.
You need to pass an agent withkeepAlive
option set totrue
. - No
proxy
option. You need to pass a custom agent. - No
auth
option.
You need to useusername
/password
instead or set theauthorization
header manually. - No
baseUrl
option.
Instead, there isprefixUrl
which appends a trailing slash if not present. - No
removeRefererHeader
option.
You can remove thereferer
header in abeforeRequest
hook. - No
followAllRedirects
option.
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-cookie
directly.
你可以开始了!¶
好吧,你已经走了这么远 看一下文档。值得花时间读一读。 这里有一些很棒的建议。
如果某件事不清楚或没有按照它应该的方式运行,不要犹豫打开一个问题.