electron/docs/api/net.md

333 lines
11 KiB
Markdown
Raw Normal View History

2016-10-18 17:14:43 +00:00
# net
2016-10-31 22:48:06 +00:00
> Issue HTTP/HTTPS requests using Chromium's native networking library
2016-10-18 17:14:43 +00:00
2016-11-03 17:26:00 +00:00
Process: [Main](../tutorial/quick-start.md#main-process)
2016-10-20 10:30:03 +00:00
The `net` module is a client-side API for issuing HTTP(S) requests. It is
2016-11-01 03:05:23 +00:00
similar to the [HTTP](https://nodejs.org/api/http.html) and
2016-10-20 11:57:08 +00:00
[HTTPS](https://nodejs.org/api/https.html) modules of Node.js but uses
2016-10-31 22:48:06 +00:00
Chromium's native networking library instead of the Node.js implementation,
offering better support for web proxies.
2016-10-20 10:30:03 +00:00
2016-10-31 22:48:06 +00:00
The following is a non-exhaustive list of why you may consider using the `net`
2016-10-20 10:30:03 +00:00
module instead of the native Node.js modules:
2016-10-31 22:48:06 +00:00
2016-10-20 10:30:03 +00:00
* Automatic management of system proxy configuration, support of the wpad
2016-11-01 03:05:23 +00:00
protocol and proxy pac configuration files.
2016-10-18 17:14:43 +00:00
* Automatic tunneling of HTTPS requests.
2016-10-20 10:30:03 +00:00
* Support for authenticating proxies using basic, digest, NTLM, Kerberos or
2016-11-01 03:05:23 +00:00
negotiate authentication schemes.
2016-10-20 10:30:03 +00:00
* Support for traffic monitoring proxies: Fiddler-like proxies used for access
2016-11-01 03:05:23 +00:00
control and monitoring.
2016-10-18 17:14:43 +00:00
2016-10-20 11:57:08 +00:00
The `net` module API has been specifically designed to mimic, as closely as
2016-10-20 10:30:03 +00:00
possible, the familiar Node.js API. The API components including classes,
methods, properties and event names are similar to those commonly used in
Node.js.
2016-10-19 16:19:28 +00:00
2016-10-20 10:30:03 +00:00
For instance, the following example quickly shows how the `net` API might be
used:
2016-10-19 16:05:38 +00:00
2016-10-18 17:14:43 +00:00
```javascript
const {app} = require('electron')
app.on('ready', () => {
2016-10-19 16:05:38 +00:00
const {net} = require('electron')
const request = net.request('https://github.com')
request.on('response', (response) => {
console.log(`STATUS: ${response.statusCode}`)
console.log(`HEADERS: ${JSON.stringify(response.headers)}`)
2016-10-19 16:05:38 +00:00
response.on('data', (chunk) => {
console.log(`BODY: ${chunk}`)
})
response.on('end', () => {
console.log('No more data in response.')
2016-10-19 16:05:38 +00:00
})
2016-10-19 13:34:21 +00:00
})
2016-10-19 16:05:38 +00:00
request.end()
2016-10-18 17:14:43 +00:00
})
```
2016-10-20 10:30:03 +00:00
By the way, it is almost identical to how you would normally use the
[HTTP](https://nodejs.org/api/http.html)/[HTTPS](https://nodejs.org/api/https.html)
modules of Node.js
2016-10-19 16:19:28 +00:00
2016-10-20 11:57:08 +00:00
The `net` API can be used only after the application emits the `ready` event.
Trying to use the module before the `ready` event will throw an error.
2016-10-18 17:14:43 +00:00
## Methods
2016-10-19 13:34:21 +00:00
The `net` module has the following methods:
### `net.request(options)`
2016-10-31 22:48:06 +00:00
* `options` (Object | String) - The `ClientRequest` constructor options.
2016-10-19 13:34:21 +00:00
Returns `ClientRequest`
2016-10-18 17:14:43 +00:00
2016-10-20 10:30:03 +00:00
Creates a `ClientRequest` instance using the provided `options` which are
directly forwarded to the `ClientRequest` constructor. The `net.request` method
would be used to issue both secure and insecure HTTP requests according to the
specified protocol scheme in the `options` object.
2016-10-19 14:51:44 +00:00
2016-10-18 17:14:43 +00:00
## Class: ClientRequest
2016-10-31 22:48:06 +00:00
> Make HTTP/HTTPS requests.
Process: [Main](../tutorial/quick-start.md#main-process)
2016-10-20 10:30:03 +00:00
`ClientRequest` implements the [Writable Stream](https://nodejs.org/api/stream.html#stream_writable_streams)
2016-10-31 22:48:06 +00:00
interface and is therefore an [EventEmitter](https://nodejs.org/api/events.html#events_class_eventemitter).
2016-10-19 14:51:44 +00:00
2016-10-19 13:34:21 +00:00
### `new ClientRequest(options)`
2016-10-31 01:43:57 +00:00
* `options` (Object | String) - If `options` is a String, it is interpreted as
2016-11-01 03:05:23 +00:00
the request URL. If it is an object, it is expected to fully specify an HTTP
request via the following properties:
2016-10-20 10:30:03 +00:00
* `method` String (optional) - The HTTP request method. Defaults to the GET
2016-11-01 03:05:23 +00:00
method.
2016-10-20 10:30:03 +00:00
* `url` String (optional) - The request URL. Must be provided in the absolute
2016-11-01 03:05:23 +00:00
form with the protocol scheme specified as http or https.
* `session` Object (optional) - The [`Session`](session.md) instance with
2016-11-01 03:05:23 +00:00
which the request is associated.
* `partition` String (optional) - The name of the [`partition`](session.md)
2016-11-01 03:05:23 +00:00
with which the request is associated. Defaults to the empty string. The
`session` option prevails on `partition`. Thus if a `session` is explicitly
specified, `partition` is ignored.
2016-10-20 10:30:03 +00:00
* `protocol` String (optional) - The protocol scheme in the form 'scheme:'.
2016-11-01 03:05:23 +00:00
Currently supported values are 'http:' or 'https:'. Defaults to 'http:'.
2016-10-20 10:30:03 +00:00
* `host` String (optional) - The server host provided as a concatenation of
2016-11-01 03:05:23 +00:00
the hostname and the port number 'hostname:port'
2016-10-19 13:34:21 +00:00
* `hostname` String (optional) - The server host name.
* `port` Integer (optional) - The server's listening port number.
2016-10-31 22:48:06 +00:00
* `path` String (optional) - The path part of the request URL.
2016-10-20 10:30:03 +00:00
`options` properties such as `protocol`, `host`, `hostname`, `port` and `path`
strictly follow the Node.js model as described in the
[URL](https://nodejs.org/api/url.html) module.
2016-10-19 13:34:21 +00:00
For instance, we could have created the same request to 'github.com' as follows:
2016-11-02 09:40:51 +00:00
```javascript
const request = net.request({
method: 'GET',
protocol: 'https:',
hostname: 'github.com',
port: 443,
path: '/'
})
```
2016-10-18 17:14:43 +00:00
### Instance Events
#### Event: 'response'
2016-10-19 13:34:21 +00:00
Returns:
2016-10-20 10:30:03 +00:00
* `response` IncomingMessage - An object representing the HTTP response message.
2016-10-19 13:34:21 +00:00
2016-10-18 17:14:43 +00:00
#### Event: 'login'
2016-10-19 13:34:21 +00:00
Returns:
2016-10-20 09:45:45 +00:00
* `authInfo` Object
* `isProxy` Boolean
* `scheme` String
* `host` String
* `port` Integer
* `realm` String
2016-10-19 13:34:21 +00:00
* `callback` Function
Emitted when an authenticating proxy is asking for user credentials.
The `callback` function is expected to be called back with user credentials:
2016-10-31 15:59:26 +00:00
* `username` String
2016-10-19 13:34:21 +00:00
* `password` String
2016-11-02 09:40:51 +00:00
```javascript
2016-10-20 09:45:45 +00:00
request.on('login', (authInfo, callback) => {
callback('username', 'password')
})
```
2016-11-01 03:05:23 +00:00
2016-10-20 10:30:03 +00:00
Providing empty credentials will cancel the request and report an authentication
error on the response object:
2016-10-20 09:45:45 +00:00
2016-11-02 09:40:51 +00:00
```javascript
2016-10-20 09:45:45 +00:00
request.on('response', (response) => {
2016-11-02 10:16:37 +00:00
console.log(`STATUS: ${response.statusCode}`)
2016-10-20 09:45:45 +00:00
response.on('error', (error) => {
console.log(`ERROR: ${JSON.stringify(error)}`)
})
})
request.on('login', (authInfo, callback) => {
callback()
})
```
2016-10-19 13:34:21 +00:00
2016-10-18 17:14:43 +00:00
#### Event: 'finish'
2016-10-20 10:30:03 +00:00
Emitted just after the last chunk of the `request`'s data has been written into
the `request` object.
2016-10-19 13:34:21 +00:00
2016-10-18 17:14:43 +00:00
#### Event: 'abort'
2016-10-20 10:30:03 +00:00
Emitted when the `request` is aborted. The `abort` event will not be fired if
the `request` is already closed.
2016-10-19 13:34:21 +00:00
2016-10-18 17:14:43 +00:00
#### Event: 'error'
2016-10-19 13:34:21 +00:00
Returns:
* `error` Error - an error object providing some information about the failure.
2016-10-20 10:30:03 +00:00
Emitted when the `net` module fails to issue a network request. Typically when
the `request` object emits an `error` event, a `close` event will subsequently
follow and no response object will be provided.
2016-10-19 13:34:21 +00:00
2016-10-18 17:14:43 +00:00
#### Event: 'close'
2016-10-20 10:30:03 +00:00
Emitted as the last event in the HTTP request-response transaction. The `close`
event indicates that no more events will be emitted on either the `request` or
`response` objects.
2016-10-19 13:34:21 +00:00
2016-10-19 14:51:44 +00:00
### Instance Properties
#### `request.chunkedEncoding`
2016-10-20 10:30:03 +00:00
A Boolean specifying whether the request will use HTTP chunked transfer encoding
or not. Defaults to false. The property is readable and writable, however it can
be set only before the first write operation as the HTTP headers are not yet put
on the wire. Trying to set the `chunkedEncoding` property after the first write
will throw an error.
2016-10-19 14:51:44 +00:00
2016-10-20 10:30:03 +00:00
Using chunked encoding is strongly recommended if you need to send a large
request body as data will be streamed in small chunks instead of being
internally buffered inside Electron process memory.
2016-10-19 14:51:44 +00:00
2016-10-18 17:14:43 +00:00
### Instance Methods
#### `request.setHeader(name, value)`
2016-10-20 10:30:03 +00:00
* `name` String - An extra HTTP header name.
* `value` String - An extra HTTP header value.
2016-10-19 13:34:21 +00:00
2016-10-20 10:30:03 +00:00
Adds an extra HTTP header. The header name will issued as it is without
lowercasing. It can be called only before first write. Calling this method after
the first write will throw an error.
2016-10-19 13:34:21 +00:00
2016-10-18 17:14:43 +00:00
#### `request.getHeader(name)`
2016-10-19 13:34:21 +00:00
* `name` String - Specify an extra header name.
Returns String - The value of a previously set extra header name.
2016-10-18 17:14:43 +00:00
#### `request.removeHeader(name)`
2016-10-19 13:34:21 +00:00
* `name` String - Specify an extra header name.
2016-10-20 10:30:03 +00:00
Removes a previously set extra header name. This method can be called only
before first write. Trying to call it after the first write will throw an error.
2016-10-19 13:34:21 +00:00
2016-10-19 14:51:44 +00:00
#### `request.write(chunk[, encoding][, callback])`
2016-10-19 13:34:21 +00:00
2016-10-31 01:43:57 +00:00
* `chunk` (String | Buffer) - A chunk of the request body's data. If it is a
2016-11-01 03:05:23 +00:00
string, it is converted into a Buffer using the specified encoding.
2016-10-20 10:30:03 +00:00
* `encoding` String (optional) - Used to convert string chunks into Buffer
2016-11-01 03:05:23 +00:00
objects. Defaults to 'utf-8'.
* `callback` Function (optional) - Called after the write operation ends.
2016-10-31 01:43:57 +00:00
2016-10-25 10:41:01 +00:00
`callback` is essentially a dummy function introduced in the purpose of keeping
similarity with the Node.js API. It is called asynchronously in the next tick
after `chunk` content have been delivered to the Chromium networking layer.
Contrary to the Node.js implementation, it is not guaranteed that `chunk`
content have been flushed on the wire before `callback` is called.
2016-10-19 13:34:21 +00:00
2016-10-20 10:30:03 +00:00
Adds a chunk of data to the request body. The first write operation may cause
the request headers to be issued on the wire. After the first write operation,
it is not allowed to add or remove a custom header.
2016-10-18 17:14:43 +00:00
2016-10-19 14:51:44 +00:00
#### `request.end([chunk][, encoding][, callback])`
2016-10-19 13:34:21 +00:00
2016-10-31 01:43:57 +00:00
* `chunk` (String | Buffer) (optional)
2016-10-19 13:34:21 +00:00
* `encoding` String (optional)
* `callback` Function (optional)
2016-10-20 10:30:03 +00:00
Sends the last chunk of the request data. Subsequent write or end operations
will not be allowed. The `finish` event is emitted just after the end operation.
2016-10-18 17:14:43 +00:00
#### `request.abort()`
2016-10-20 10:30:03 +00:00
Cancels an ongoing HTTP transaction. If the request has already emitted the
`close` event, the abort operation will have no effect. Otherwise an ongoing
event will emit `abort` and `close` events. Additionally, if there is an ongoing
response object,it will emit the `aborted` event.
2016-10-19 13:34:21 +00:00
2016-10-18 17:14:43 +00:00
## Class: IncomingMessage
2016-10-31 22:48:06 +00:00
> Handle responses to HTTP/HTTPS requests.
Process: [Main](../tutorial/quick-start.md#main-process)
2016-10-31 22:48:06 +00:00
`IncomingMessage` implements the [Readable Stream](https://nodejs.org/api/stream.html#stream_readable_streams)
interface and is therefore an [EventEmitter](https://nodejs.org/api/events.html#events_class_eventemitter).
2016-10-19 14:51:44 +00:00
2016-10-19 13:34:21 +00:00
### Instance Events
2016-10-31 22:48:06 +00:00
#### Event: 'data'
2016-10-19 13:34:21 +00:00
2016-10-19 14:51:44 +00:00
Returns:
2016-10-31 22:48:06 +00:00
* `chunk` Buffer - A chunk of response body's data.
2016-10-19 14:51:44 +00:00
2016-10-20 10:30:03 +00:00
The `data` event is the usual method of transferring response data into
2016-10-31 22:48:06 +00:00
applicative code.
2016-10-19 16:31:08 +00:00
2016-10-31 22:48:06 +00:00
#### Event: 'end'
2016-10-19 13:34:21 +00:00
2016-10-19 14:51:44 +00:00
Indicates that response body has ended.
2016-10-31 22:48:06 +00:00
#### Event: 'aborted'
2016-10-19 13:34:21 +00:00
2016-10-19 14:51:44 +00:00
Emitted when a request has been canceled during an ongoing HTTP transaction.
2016-10-31 22:48:06 +00:00
#### Event: 'error'
2016-10-19 13:34:21 +00:00
2016-10-20 10:30:03 +00:00
Returns:
2016-10-19 14:51:44 +00:00
2016-10-19 16:31:08 +00:00
`error` Error - Typically holds an error string identifying failure root cause.
2016-10-19 14:51:44 +00:00
2016-10-20 10:30:03 +00:00
Emitted when an error was encountered while streaming response data events. For
instance, if the server closes the underlying while the response is still
streaming, an `error` event will be emitted on the response object and a `close`
event will subsequently follow on the request object.
2016-10-19 14:51:44 +00:00
2016-10-31 22:48:06 +00:00
### Instance Properties
2016-10-19 13:34:21 +00:00
2016-10-19 14:51:44 +00:00
An `IncomingMessage` instance has the following readable properties:
2016-10-19 13:34:21 +00:00
#### `response.statusCode`
2016-10-19 14:51:44 +00:00
An Integer indicating the HTTP response status code.
2016-10-19 13:34:21 +00:00
#### `response.statusMessage`
2016-10-19 14:51:44 +00:00
A String representing the HTTP status message.
2016-10-19 13:34:21 +00:00
#### `response.headers`
2016-10-20 10:30:03 +00:00
An Object representing the response HTTP headers. The `headers` object is
formatted as follows:
2016-10-19 16:05:38 +00:00
* All header names are lowercased.
* Each header name produces an array-valued property on the headers object.
* Each header value is pushed into the array associated with its header name.
2016-10-19 14:51:44 +00:00
2016-10-19 13:34:21 +00:00
#### `response.httpVersion`
2016-10-20 10:30:03 +00:00
A String indicating the HTTP protocol version number. Typical values are '1.0'
or '1.1'. Additionally `httpVersionMajor` and `httpVersionMinor` are two
Integer-valued readable properties that return respectively the HTTP major and
minor version numbers.