# 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 preambleCRLF or postamble by passing them as `true` to your request options.
```js
request({
method: 'PUT',
preambleCRLF: true,
postambleCRLF: true,
uri: 'http://service.com/upload',
multipart: [
没有合适的资源?快使用搜索试试~ 我知道了~
serialport-linux.zip
共1351个文件
js:542个
json:217个
md:215个
需积分: 9 2 下载量 85 浏览量
2019-10-29
10:57:04
上传
评论
收藏 2.22MB ZIP 举报
温馨提示
nodejs调用串口的依赖模块,这个是源码,已经在linux平台上编译过,如果要在windows平台使用,下载源码后进入解压目录,执行:node-gyp configure 和 node-gyp build即可完成编译
资源推荐
资源详情
资源评论
收起资源包目录
serialport-linux.zip (1351个子文件)
almond.0 2KB
sshpk-conv.1 4KB
sshpk-sign.1 2KB
sshpk-verify.1 2KB
almond.1 558B
address 692B
LICENSE.APACHE2 586B
AUTHORS 217B
AUTHORS 217B
range.bnf 629B
LICENSE.BSD 1KB
disphelper.c 34KB
benchmark-native.c 613B
calendar 998B
card 2KB
node-pre-gyp.cmd 40B
jsl.node.conf 7KB
jsl.node.conf 7KB
enumser.cpp 47KB
serialport_unix.cpp 21KB
serialport.cpp 19KB
serialport_win.cpp 16KB
serialport_poller.cpp 3KB
serialport_unix.o.d 4KB
serialport_poller.o.d 4KB
serialport.o.d 4KB
serialport.node.d 337B
serialport.node.d 137B
Makefile.deps 1KB
.editorconfig 276B
.editorconfig 276B
.dir-locals.el 178B
.eslintignore 5B
.eslintrc 5KB
.eslintrc 1KB
.eslintrc 556B
.eslintrc 505B
.eslintrc 426B
.eslintrc 184B
.eslintrc 172B
.eslintrc 156B
.eslintrc 91B
geo 173B
.gitmodules 205B
.gitmodules 0B
bench.gnu 6KB
binding.gyp 1KB
packed.tar.gz 152B
stdstring.h 133KB
nan.h 66KB
disphelper.h 21KB
nan_callbacks_12_inl.h 17KB
nan_callbacks_pre_12_inl.h 16KB
nan_weak.h 15KB
nan_implementation_12_inl.h 14KB
nan_new.h 9KB
nan_implementation_pre_12_inl.h 8KB
nan_string_bytes.h 8KB
nan_maybe_pre_43_inl.h 8KB
nan_maybe_43_inl.h 6KB
enumser.h 6KB
nan_persistent_pre_12_inl.h 6KB
nan_object_wrap.h 4KB
serialport.h 4KB
nan_persistent_12_inl.h 4KB
nan_callbacks.h 3KB
nan_typedarray_contents.h 3KB
AutoHeapAlloc.h 2KB
AutoHModule.h 2KB
nan_converters.h 2KB
AutoHandle.h 1KB
nan_converters_pre_43_inl.h 1KB
nan_converters_43_inl.h 1KB
stdafx.h 1KB
serialport_poller.h 751B
har-validator 2KB
index.html 542B
test.html 340B
example.html 200B
hyper-schema 1KB
hyper-schema 1KB
hyper-schema 1KB
hyper-schema 1002B
hyper-schema 996B
dashdash.bash_completion.in 14KB
interfaces 868B
pubsuffix.js 146KB
index.js 66KB
nacl-fast.js 61KB
browser.js 59KB
server.js 50KB
index.js 41KB
request.js 40KB
cookie.js 36KB
dashdash.js 34KB
semver.js 32KB
nacl.js 32KB
nacl-fast.min.js 32KB
uri.js 29KB
_stream_readable.js 27KB
共 1351 条
- 1
- 2
- 3
- 4
- 5
- 6
- 14
资源评论
password-u
- 粉丝: 94
- 资源: 86
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功