<div align="center">
<br>
<br>
<img width="360" src="media/logo.svg" alt="Got">
<br>
<br>
<br>
<p align="center">Huge thanks to <a href="https://moxy.studio"><img src="https://sindresorhus.com/assets/thanks/moxy-logo.svg" width="150"></a> for sponsoring me!
</p>
<br>
<br>
</div>
> Simplified HTTP requests
[![Build Status: Linux](https://travis-ci.org/sindresorhus/got.svg?branch=master)](https://travis-ci.org/sindresorhus/got) [![Coverage Status](https://coveralls.io/repos/github/sindresorhus/got/badge.svg?branch=master)](https://coveralls.io/github/sindresorhus/got?branch=master) [![Downloads](https://img.shields.io/npm/dm/got.svg)](https://npmjs.com/got) [![Install size](https://packagephobia.now.sh/badge?p=got)](https://packagephobia.now.sh/result?p=got)
Got is a human-friendly and powerful HTTP request library.
It was created because the popular [`request`](https://github.com/request/request) package is bloated: [![Install size](https://packagephobia.now.sh/badge?p=request)](https://packagephobia.now.sh/result?p=request)
Got is for Node.js. For browsers, we recommend [Ky](https://github.com/sindresorhus/ky).
## Highlights
- [Promise & stream API](#api)
- [Request cancelation](#aborting-the-request)
- [RFC compliant caching](#cache-adapters)
- [Follows redirects](#followredirect)
- [Retries on failure](#retry)
- [Progress events](#onuploadprogress-progress)
- [Handles gzip/deflate](#decompress)
- [Timeout handling](#timeout)
- [Errors with metadata](#errors)
- [JSON mode](#json)
- [WHATWG URL support](#url)
- [Hooks](#hooks)
- [Instances with custom defaults](#instances)
- [Composable](advanced-creation.md#merging-instances)
- [Electron support](#useelectronnet)
- [Used by ~2000 packages and ~500K repos](https://github.com/sindresorhus/got/network/dependents)
- Actively maintained
[Moving from Request?](migration-guides.md)
[See how Got compares to other HTTP libraries](#comparison)
## Install
```
$ npm install got
```
<a href="https://www.patreon.com/sindresorhus">
<img src="https://c5.patreon.com/external/logo/become_a_patron_button@2x.png" width="160">
</a>
## Usage
```js
const got = require('got');
(async () => {
try {
const response = await got('sindresorhus.com');
console.log(response.body);
//=> '<!doctype html> ...'
} catch (error) {
console.log(error.response.body);
//=> 'Internal server error ...'
}
})();
```
###### Streams
```js
const fs = require('fs');
const got = require('got');
got.stream('sindresorhus.com').pipe(fs.createWriteStream('index.html'));
// For POST, PUT, and PATCH methods `got.stream` returns a `stream.Writable`
fs.createReadStream('index.html').pipe(got.stream.post('sindresorhus.com'));
```
### API
It's a `GET` request by default, but can be changed by using different methods or in the `options`.
#### got(url, [options])
Returns a Promise for a [`response` object](#response) or a [stream](#streams-1) if `options.stream` is set to true.
##### url
Type: `string` `Object`
The URL to request, as a string, a [`https.request` options object](https://nodejs.org/api/https.html#https_https_request_options_callback), or a [WHATWG `URL`](https://nodejs.org/api/url.html#url_class_url).
Properties from `options` will override properties in the parsed `url`.
If no protocol is specified, it will default to `https`.
##### options
Type: `Object`
Any of the [`https.request`](https://nodejs.org/api/https.html#https_https_request_options_callback) options.
###### baseUrl
Type: `string` `Object`
When specified, `url` will be prepended by `baseUrl`.<br>
If you specify an absolute URL, it will skip the `baseUrl`.
Very useful when used with `got.extend()` to create niche-specific Got instances.
Can be a string or a [WHATWG `URL`](https://nodejs.org/api/url.html#url_class_url).
Slash at the end of `baseUrl` and at the beginning of the `url` argument is optional:
```js
await got('hello', {baseUrl: 'https://example.com/v1'});
//=> 'https://example.com/v1/hello'
await got('/hello', {baseUrl: 'https://example.com/v1/'});
//=> 'https://example.com/v1/hello'
await got('/hello', {baseUrl: 'https://example.com/v1'});
//=> 'https://example.com/v1/hello'
```
###### headers
Type: `Object`<br>
Default: `{}`
Request headers.
Existing headers will be overwritten. Headers set to `null` will be omitted.
###### stream
Type: `boolean`<br>
Default: `false`
Returns a `Stream` instead of a `Promise`. This is equivalent to calling `got.stream(url, [options])`.
###### body
Type: `string` `Buffer` `stream.Readable` [`form-data` instance](https://github.com/form-data/form-data)
**Note:** If you provide this option, `got.stream()` will be read-only.
The body that will be sent with a `POST` request.
If present in `options` and `options.method` is not set, `options.method` will be set to `POST`.
The `content-length` header will be automatically set if `body` is a `string` / `Buffer` / `fs.createReadStream` instance / [`form-data` instance](https://github.com/form-data/form-data), and `content-length` and `transfer-encoding` are not manually set in `options.headers`.
###### cookieJar
Type: [`tough.CookieJar` instance](https://github.com/salesforce/tough-cookie#cookiejar)
**Note:** If you provide this option, `options.headers.cookie` will be overridden.
Cookie support. You don't have to care about parsing or how to store them. [Example.](#cookies)
###### encoding
Type: `string` `null`<br>
Default: `'utf8'`
[Encoding](https://nodejs.org/api/buffer.html#buffer_buffers_and_character_encodings) to be used on `setEncoding` of the response data. If `null`, the body is returned as a [`Buffer`](https://nodejs.org/api/buffer.html) (binary data).
###### form
Type: `boolean`<br>
Default: `false`
**Note:** If you provide this option, `got.stream()` will be read-only.
**Note:** `body` must be a plain object. It will be converted to a query string using [`(new URLSearchParams(object)).toString()`](https://nodejs.org/api/url.html#url_constructor_new_urlsearchparams_obj).
If set to `true` and `Content-Type` header is not set, it will be set to `application/x-www-form-urlencoded`.
###### json
Type: `boolean`<br>
Default: `false`
**Note:** If you use `got.stream()`, this option will be ignored.
**Note:** `body` must be a plain object or array and will be stringified.
If set to `true` and `Content-Type` header is not set, it will be set to `application/json`.
Parse response body with `JSON.parse` and set `accept` header to `application/json`. If used in conjunction with the `form` option, the `body` will the stringified as querystring and the response parsed as JSON.
###### query
Type: `string` `Object<string, string|number>` [`URLSearchParams`](https://developer.mozilla.org/en-US/docs/Web/API/URLSearchParams)
Query string that will be added to the request URL. This will override the query string in `url`.
If you need to pass in an array, you can do it using a `URLSearchParams` instance:
```js
const got = require('got');
const query = new URLSearchParams([['key', 'a'], ['key', 'b']]);
got('https://example.com', {query});
console.log(query.toString());
//=> 'key=a&key=b'
```
And if you need a different array format, you could use the [`query-string`](https://github.com/sindresorhus/query-string) package:
```js
const got = require('got');
const queryString = require('query-string');
const query = queryString.stringify({key: ['a', 'b']}, {arrayFormat: 'bracket'});
got('https://example.com', {query});
console.log(query);
//=> 'key[]=a&key[]=b'
```
###### timeout
Type: `number` `Object`
Milliseconds to wait for the server to end the response before aborting the request with [`got.TimeoutError`](#gottimeouterror) error (a.k.a. `request` property). By default, there's no timeout.
This also accepts an `object` with the following fields to constrain the duration of each phase of the request lifecycle:
- `lookup` starts when a socket is assigned and ends when the hostname has been resolved
没有合适的资源?快使用搜索试试~ 我知道了~
blog-2.zip用node开发的博客后台
共798个文件
js:291个
md:134个
json:122个
需积分: 5 0 下载量 90 浏览量
2021-12-31
17:27:46
上传
评论
收藏 985KB ZIP 举报
温馨提示
nodejs开发博客后台
资源详情
资源评论
资源推荐
收起资源包目录
blog-2.zip用node开发的博客后台 (798个子文件)
LICENSE.APACHE2 586B
range.bnf 619B
range.bnf 619B
range.bnf 619B
range.bnf 619B
range.bnf 619B
LICENSE.BSD 1KB
nodetouch.cmd 192B
nodemon.cmd 192B
semver.cmd 188B
semver.cmd 188B
semver.cmd 188B
semver.cmd 188B
semver.cmd 182B
nopt.cmd 180B
is-ci.cmd 172B
rc.cmd 166B
windows-kill.exe 79KB
is-ci 295B
semver.js 41KB
semver.js 41KB
semver.js 41KB
semver.js 38KB
index.js 28KB
parse.js 27KB
minimatch.js 25KB
index.js 23KB
nodefs-handler.js 20KB
conversions.js 17KB
run.js 16KB
fsevents-handler.js 16KB
nopt.js 15KB
range.js 14KB
graceful-fs.js 12KB
text.js 11KB
index.js 11KB
index.js 11KB
text.js 10KB
index.js 10KB
picomatch.js 10KB
polyfills.js 10KB
scan.js 9KB
index.js 9KB
request-as-event-emitter.js 9KB
nodemon.js 9KB
yallist.js 8KB
index.js 8KB
index.js 8KB
semver.js 8KB
index.js 7KB
load.js 7KB
normalize-arguments.js 7KB
subset.js 7KB
index.js 7KB
parse.js 7KB
exec.js 7KB
index.js 7KB
match.js 7KB
re.js 7KB
index.js 6KB
watch.js 6KB
index.js 6KB
browser.js 6KB
index.js 6KB
index.js 6KB
parse.js 6KB
common.js 6KB
index.js 6KB
index.js 6KB
index.js 5KB
ini.js 5KB
index.js 5KB
ini.js 5KB
index.js 5KB
semver.js 5KB
semver.js 5KB
semver.js 5KB
index.js 5KB
parse.js 4KB
constants.js 4KB
timed-out.js 4KB
node.js 4KB
imurmurhash.js 4KB
index.js 4KB
semver.js 4KB
deep-extend.js 4KB
bool.js 4KB
index.js 4KB
index.js 4KB
index.js 4KB
comparator.js 4KB
templates.js 3KB
index.js 3KB
index.js 3KB
index.js 3KB
index.js 3KB
index.js 3KB
as-promise.js 3KB
errors.js 3KB
index.js 3KB
共 798 条
- 1
- 2
- 3
- 4
- 5
- 6
- 8
゛躲三生
- 粉丝: 10
- 资源: 44
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论0