# 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 Text3 64位最新版本-含ftp插件-SVN插件.rar
共9308个文件
js:4598个
sublime-settings:854个
png:675个
5星 · 超过95%的资源 需积分: 14 19 下载量 21 浏览量
2019-05-15
23:35:39
上传
评论 2
收藏 62.68MB RAR 举报
温馨提示
PHP程序开发必备工具,Sublime Text3 64位最新版本,含ftp/sftp插件,SVN插件,代码格式化等功能
资源推荐
资源详情
资源评论
收起资源包目录
Sublime Text3 64位最新版本-含ftp插件-SVN插件.rar (9308个子文件)
almond.0 2KB
npm-install.1 15KB
npm-shrinkwrap.1 7KB
npm.1 6KB
npm-README.1 5KB
npm-doctor.1 5KB
npm-version.1 5KB
npm-update.1 4KB
npm-outdated.1 4KB
sshpk-conv.1 4KB
npm-view.1 3KB
npm-search.1 3KB
npm-dist-tag.1 3KB
npm-adduser.1 3KB
npm-link.1 3KB
npm-run-script.1 3KB
npm-access.1 3KB
npm-ls.1 3KB
npm-publish.1 3KB
sshpk-sign.1 3KB
npm-cache.1 2KB
sshpk-verify.1 2KB
marked.1 2KB
npm-team.1 2KB
npm-config.1 2KB
npm-uninstall.1 2KB
npm-dedupe.1 2KB
npm-logout.1 1KB
npm-docs.1 1KB
npm-unpublish.1 1KB
npm-init.1 1KB
npm-bugs.1 1KB
npm-owner.1 1KB
npm-edit.1 1KB
npm-help.1 1KB
npm-explore.1 1KB
npm-restart.1 1KB
npm-help-search.1 971B
npm-completion.1 961B
npm-prune.1 889B
npm-pack.1 877B
npm-deprecate.1 864B
npm-repo.1 811B
npm-install-test.1 790B
npm-start.1 779B
npm-build.1 682B
npm-prefix.1 655B
npm-star.1 576B
npm-stars.1 571B
almond.1 571B
npm-rebuild.1 493B
npm-bin.1 444B
npm-root.1 439B
npm-test.1 437B
npm-bundle.1 429B
npm-stop.1 414B
npm-whoami.1 401B
npm-ping.1 379B
npm-json.5 27KB
package.json.5 27KB
npm-folders.5 9KB
npm-global.5 9KB
npmrc.5 3KB
npm-config.7 32KB
semver.7 16KB
npm-scripts.7 11KB
npm-developers.7 8KB
npm-disputes.7 6KB
npm-coding-style.7 6KB
npm-scope.7 4KB
npm-registry.7 4KB
npm-index.7 4KB
npm-orgs.7 3KB
removing-npm.7 2KB
acorn 2KB
acorn 301B
address 692B
LICENSE.APACHE2 601B
LICENSE.APACHE2 601B
AUTHORS 17KB
AUTHORS 851B
AUTHORS 339B
AUTHORS 223B
AUTHORS 223B
AUTHORS 174B
AUTHORS 154B
.babelrc 144B
!添加∕删除右键菜单.bat 851B
nodevars.bat 702B
gyp.bat 201B
samples.bat 196B
make.bat 159B
range.bnf 645B
build 134B
builtin 23B
imesupport_hook.c 4KB
Package Control.ca-bundle 3KB
Package Control.ca-list 77B
Startup.cache 561KB
PHP Source.sublime-syntax.cache 29KB
共 9308 条
- 1
- 2
- 3
- 4
- 5
- 6
- 94
资源评论
- wangwwwwws2019-08-02不错不错 满足我需求
黑米网络
- 粉丝: 6
- 资源: 17
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功