Axios¶
Axios与Got非常相似。区别在于Axios首先针对浏览器,而Got则充分利用了Node.js的特性。
常见的选项¶
这些选项也保持不变:
重命名选项¶
我们非常关心可读性,所以我们重命名了这些选项:
httpAgent
→agent.http
httpsAgent
→agent.https
socketPath
→url
responseEncoding
→encoding
auth.username
→username
auth.password
→password
data
→body
/json
/form
params
→searchParams
行为的改变¶
transformRequest
→hooks.beforeRequest
- The API is different.
transformResponse
→hooks.afterResponse
- The API is different.
baseUrl
→prefixUrl
- The
prefixUrl
is always prepended to theurl
. timeout
- This option is now an object. You can now set timeouts on particular events!
responseType
- Accepts
'text'
,'json'
or'buffer'
.
突发的变化¶
onUploadProgress
- This option does not exist. Instead, use
got(…).on('uploadProgress', …)
. onDownloadProgress
- This option does not exist. Instead, use
got(…).on('downloadProgress', …)
. maxContentLength
- This option does not exist. Instead, use a handler.
validateStatus
- This option does not exist. Got automatically validates the status according to the specification.
proxy
- This option does not exist. You need to pass an
agent
instead. cancelToken
- This option does not exist, but will be implemented soon. For now, use
promise.cancel()
orstream.destroy()
. paramsSerializer
- This option does not exist.
maxBodyLength
- This option does not exist.
响应¶
响应对象也不同:
response.data
→response.body
response.status
→response.statusCode
response.statusText
→response.statusMessage
response.config
→response.request.options
response.request
- Returns a Got stream.
The response.headers
object remains the same.
拦截器¶
而是提供了hooks,这更灵活。
错误¶
错误看起来是一样的,不同的是error.request
返回一个get流。
此外,Got还提供了更多细节,使调试更容易。
取消¶
虽然Got不支持AbortController
,你可以使用promise.cancel()
。
方便的方法¶
方便的方法,如axios.get(…)
等保持不变:got.get(…)
。
而不是axios.create(…)
使用 got.extend(…)
。
你可以走了!¶
好吧,你已经走了这么远了
请查看文档。值得花时间读一读
这里有一些很好的建议(../tips.md)。
如果有些事情不清楚或没有正常工作,不要犹豫提出问题.