# Request - Simplified HTTP client
[![npm package](https://nodei.co/npm/request.png?downloads=true&downloadRank=true&stars=true)](https://nodei.co/npm/request/)
[![Build status](https://img.shields.io/travis/request/request/master.svg?style=flat-square)](https://travis-ci.org/request/request)
[![Coverage](https://img.shields.io/codecov/c/github/request/request.svg?style=flat-square)](https://codecov.io/github/request/request?branch=master)
[![Coverage](https://img.shields.io/coveralls/request/request.svg?style=flat-square)](https://coveralls.io/r/request/request)
[![Dependency Status](https://img.shields.io/david/request/request.svg?style=flat-square)](https://david-dm.org/request/request)
[![Known Vulnerabilities](https://snyk.io/test/npm/request/badge.svg?style=flat-square)](https://snyk.io/test/npm/request)
[![Gitter](https://img.shields.io/badge/gitter-join_chat-blue.svg?style=flat-square)](https://gitter.im/request/request?utm_source=badge)
## Super simple to use
Request is designed to be the simplest way possible to make http calls. It supports HTTPS and follows redirects by default.
```js
var request = require('request');
request('http://www.google.com', function (error, response, body) {
if (!error && response.statusCode == 200) {
console.log(body) // Show the HTML for the Google homepage.
}
})
```
## Table of contents
- [Streaming](#streaming)
- [Forms](#forms)
- [HTTP Authentication](#http-authentication)
- [Custom HTTP Headers](#custom-http-headers)
- [OAuth Signing](#oauth-signing)
- [Proxies](#proxies)
- [Unix Domain Sockets](#unix-domain-sockets)
- [TLS/SSL Protocol](#tlsssl-protocol)
- [Support for HAR 1.2](#support-for-har-12)
- [**All Available Options**](#requestoptions-callback)
Request also offers [convenience methods](#convenience-methods) like
`request.defaults` and `request.post`, and there are
lots of [usage examples](#examples) and several
[debugging techniques](#debugging).
---
## Streaming
You can stream any response to a file stream.
```js
request('http://google.com/doodle.png').pipe(fs.createWriteStream('doodle.png'))
```
You can also stream a file to a PUT or POST request. This method will also check the file extension against a mapping of file extensions to content-types (in this case `application/json`) and use the proper `content-type` in the PUT request (if the headers don’t already provide one).
```js
fs.createReadStream('file.json').pipe(request.put('http://mysite.com/obj.json'))
```
Request can also `pipe` to itself. When doing so, `content-type` and `content-length` are preserved in the PUT headers.
```js
request.get('http://google.com/img.png').pipe(request.put('http://mysite.com/img.png'))
```
Request emits a "response" event when a response is received. The `response` argument will be an instance of [http.IncomingMessage](https://nodejs.org/api/http.html#http_class_http_incomingmessage).
```js
request
.get('http://google.com/img.png')
.on('response', function(response) {
console.log(response.statusCode) // 200
console.log(response.headers['content-type']) // 'image/png'
})
.pipe(request.put('http://mysite.com/img.png'))
```
To easily handle errors when streaming requests, listen to the `error` event before piping:
```js
request
.get('http://mysite.com/doodle.png')
.on('error', function(err) {
console.log(err)
})
.pipe(fs.createWriteStream('doodle.png'))
```
Now let’s get fancy.
```js
http.createServer(function (req, resp) {
if (req.url === '/doodle.png') {
if (req.method === 'PUT') {
req.pipe(request.put('http://mysite.com/doodle.png'))
} else if (req.method === 'GET' || req.method === 'HEAD') {
request.get('http://mysite.com/doodle.png').pipe(resp)
}
}
})
```
You can also `pipe()` from `http.ServerRequest` instances, as well as to `http.ServerResponse` instances. The HTTP method, headers, and entity-body data will be sent. Which means that, if you don't really care about security, you can do:
```js
http.createServer(function (req, resp) {
if (req.url === '/doodle.png') {
var x = request('http://mysite.com/doodle.png')
req.pipe(x)
x.pipe(resp)
}
})
```
And since `pipe()` returns the destination stream in ≥ Node 0.5.x you can do one line proxying. :)
```js
req.pipe(request('http://mysite.com/doodle.png')).pipe(resp)
```
Also, none of this new functionality conflicts with requests previous features, it just expands them.
```js
var r = request.defaults({'proxy':'http://localproxy.com'})
http.createServer(function (req, resp) {
if (req.url === '/doodle.png') {
r.get('http://google.com/doodle.png').pipe(resp)
}
})
```
You can still use intermediate proxies, the requests will still follow HTTP forwards, etc.
[back to top](#table-of-contents)
---
## Forms
`request` supports `application/x-www-form-urlencoded` and `multipart/form-data` form uploads. For `multipart/related` refer to the `multipart` API.
#### application/x-www-form-urlencoded (URL-Encoded Forms)
URL-encoded forms are simple.
```js
request.post('http://service.com/upload', {form:{key:'value'}})
// or
request.post('http://service.com/upload').form({key:'value'})
// or
request.post({url:'http://service.com/upload', form: {key:'value'}}, function(err,httpResponse,body){ /* ... */ })
```
#### multipart/form-data (Multipart Form Uploads)
For `multipart/form-data` we use the [form-data](https://github.com/form-data/form-data) library by [@felixge](https://github.com/felixge). For the most cases, you can pass your upload form data via the `formData` option.
```js
var formData = {
// Pass a simple key-value pair
my_field: 'my_value',
// Pass data via Buffers
my_buffer: new Buffer([1, 2, 3]),
// Pass data via Streams
my_file: fs.createReadStream(__dirname + '/unicycle.jpg'),
// Pass multiple values /w an Array
attachments: [
fs.createReadStream(__dirname + '/attachment1.jpg'),
fs.createReadStream(__dirname + '/attachment2.jpg')
],
// Pass optional meta-data with an 'options' object with style: {value: DATA, options: OPTIONS}
// Use case: for some types of streams, you'll need to provide "file"-related information manually.
// See the `form-data` README for more information about options: https://github.com/form-data/form-data
custom_file: {
value: fs.createReadStream('/dev/urandom'),
options: {
filename: 'topsecret.jpg',
contentType: 'image/jpg'
}
}
};
request.post({url:'http://service.com/upload', formData: formData}, function optionalCallback(err, httpResponse, body) {
if (err) {
return console.error('upload failed:', err);
}
console.log('Upload successful! Server responded with:', body);
});
```
For advanced cases, you can access the form-data object itself via `r.form()`. This can be modified until the request is fired on the next cycle of the event-loop. (Note that this calling `form()` will clear the currently set form data for that request.)
```js
// NOTE: Advanced use-case, for normal use see 'formData' usage above
var r = request.post('http://service.com/upload', function optionalCallback(err, httpResponse, body) {...})
var form = r.form();
form.append('my_field', 'my_value');
form.append('my_buffer', new Buffer([1, 2, 3]));
form.append('custom_file', fs.createReadStream(__dirname + '/unicycle.jpg'), {filename: 'unicycle.jpg'});
```
See the [form-data README](https://github.com/form-data/form-data) for more information & examples.
#### multipart/related
Some variations in different HTTP implementations require a newline/CRLF before, after, or both before and after the boundary of a `multipart/related` request (using the multipart option). This has been observed in the .NET WebAPI version 4.0. You can turn on a boundary preambl
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
Sublime Text 是一个轻量级、跨平台的文本和源代码编辑器,它支持多种编程语言和标记语言。Sublime Text 的主要功能和作用包括: 1.代码编辑:Sublime Text 提供了强大的代码编辑功能,包括语法高亮、代码折叠、自动完成、代码缩进和格式化等,这些功能有助于提高编程效率和代码的可读性。 2.多语言支持:Sublime Text 支持多种编程和标记语言,如 Python、JavaScript、HTML、CSS、Markdown 等,几乎适用于所有的软件开发需求。 3.插件扩展:Sublime Text 有一个丰富的插件生态系统,用户可以通过安装插件来扩展编辑器的功能,如版本控制集成、代码调试、语法检查等。 4.跨平台:Sublime Text 可在 Windows、macOS 和 Linux 操作系统上运行,用户可以在不同的设备上使用相同的编辑器。 5.高性能:Sublime Text 设计轻巧,启动速度快,即使是在打开大型文件时也能保持良好的性能。 6.自定义功能:用户可以自定义编辑器的界面、快捷键、编辑行为等,以适应个人的编码习惯和偏好
资源推荐
资源详情
资源评论
收起资源包目录
Sublime Text 是一个轻量级、跨平台的文本和源代码编辑器,它支持多种编程语言和标记语言 (2000个子文件)
tocdoc.css 124KB
default.css 7KB
extra-06017885a7.css 6KB
jsdoc-default.css 6KB
extra-c507967d85.css 5KB
qunit.css 5KB
style.css 2KB
demo.css 2KB
prettify-tomorrow.css 2KB
style.css 2KB
prettify-jsdoc.css 1KB
not-json.css 782B
style.css 0B
index.html 92KB
demo.html 9KB
test.html 5KB
taffy-test.html 2KB
index.html 2KB
dist-min.html 1KB
dist-concat.html 1KB
test-apart-ctx.html 606B
foo.html 0B
parser.js 293KB
jquery.js 247KB
bluebird.js 174KB
acorn.js 116KB
bluebird.core.js 116KB
acorn.es.js 110KB
bluebird.min.js 77KB
test.js 67KB
taffy.js 62KB
underscore-contrib.js 58KB
qunit.js 56KB
bluebird.core.min.js 54KB
underscore.js 52KB
underscore.js 44KB
underscore.js 44KB
underscore.js 44KB
acorn_loose.js 44KB
acorn_loose.es.js 40KB
semver.js 33KB
marked.js 32KB
index.js 30KB
templateHelper.js 30KB
debuggability.js 29KB
_stream_readable.js 29KB
parser.generated.js 26KB
install.js 26KB
minimatch.js 26KB
minimatch.js 26KB
minimatch.js 26KB
minimatch.js 26KB
definitions.js 26KB
promise.js 25KB
expression.js 25KB
espree.js 25KB
deps.js 25KB
tokenize.js 22KB
index.js 22KB
pack.js 22KB
statement.js 22KB
test-process-release.js 22KB
publish.js 21KB
visitor.js 20KB
pack-no-proprietary.js 20KB
jslitmus.js 20KB
index.js 20KB
augment.js 19KB
glob.js 19KB
glob.js 19KB
parse.js 19KB
schema.js 18KB
marked.min.js 18KB
parser.js 18KB
underscore-contrib.min.js 17KB
configure.js 17KB
_stream_writable.js 16KB
add-remote-git.js 16KB
underscore-min.js 16KB
expression.js 16KB
astnode.js 16KB
js2xmlparser.js 15KB
install.js 15KB
walker.js 15KB
tar.js 15KB
name.js 15KB
ls.js 14KB
document.js 14KB
underscore-min.js 14KB
underscore-min.js 14KB
function.predicates.js 14KB
describe.js 14KB
statement.js 14KB
prettify.js 13KB
inject.js 13KB
walk.js 13KB
lifecycle.js 13KB
doclet.js 13KB
npm.js 12KB
outdated.js 12KB
共 2000 条
- 1
- 2
- 3
- 4
- 5
- 6
- 20
资源评论
深度学习0407
- 粉丝: 1720
- 资源: 6
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功