|
| 1 | +## Class: ClientRequest |
| 2 | + |
| 3 | +> Make HTTP/HTTPS requests. |
| 4 | +
|
| 5 | +Process: [Main](../tutorial/quick-start.md#main-process) |
| 6 | + |
| 7 | +`ClientRequest` implements the [Writable Stream](https://nodejs.org/api/stream.html#stream_writable_streams) |
| 8 | +interface and is therefore an [EventEmitter](https://nodejs.org/api/events.html#events_class_eventemitter). |
| 9 | + |
| 10 | +### `new ClientRequest(options)` |
| 11 | + |
| 12 | +* `options` (Object | String) - If `options` is a String, it is interpreted as |
| 13 | +the request URL. If it is an object, it is expected to fully specify an HTTP request via the |
| 14 | +following properties: |
| 15 | + * `method` String (optional) - The HTTP request method. Defaults to the GET |
| 16 | +method. |
| 17 | + * `url` String (optional) - The request URL. Must be provided in the absolute |
| 18 | +form with the protocol scheme specified as http or https. |
| 19 | + * `session` Object (optional) - The [`Session`](session.md) instance with |
| 20 | +which the request is associated. |
| 21 | + * `partition` String (optional) - The name of the [`partition`](session.md) |
| 22 | + with which the request is associated. Defaults to the empty string. The |
| 23 | +`session` option prevails on `partition`. Thus if a `session` is explicitly |
| 24 | +specified, `partition` is ignored. |
| 25 | + * `protocol` String (optional) - The protocol scheme in the form 'scheme:'. |
| 26 | +Currently supported values are 'http:' or 'https:'. Defaults to 'http:'. |
| 27 | + * `host` String (optional) - The server host provided as a concatenation of |
| 28 | +the hostname and the port number 'hostname:port' |
| 29 | + * `hostname` String (optional) - The server host name. |
| 30 | + * `port` Integer (optional) - The server's listening port number. |
| 31 | + * `path` String (optional) - The path part of the request URL. |
| 32 | + |
| 33 | +`options` properties such as `protocol`, `host`, `hostname`, `port` and `path` |
| 34 | +strictly follow the Node.js model as described in the |
| 35 | +[URL](https://nodejs.org/api/url.html) module. |
| 36 | + |
| 37 | +For instance, we could have created the same request to 'github.com' as follows: |
| 38 | + |
| 39 | +```JavaScript |
| 40 | +const request = net.request({ |
| 41 | + method: 'GET', |
| 42 | + protocol: 'https:', |
| 43 | + hostname: 'github.com', |
| 44 | + port: 443, |
| 45 | + path: '/' |
| 46 | +}) |
| 47 | +``` |
| 48 | + |
| 49 | +### Instance Events |
| 50 | + |
| 51 | +#### Event: 'response' |
| 52 | + |
| 53 | +Returns: |
| 54 | + |
| 55 | +* `response` IncomingMessage - An object representing the HTTP response message. |
| 56 | + |
| 57 | +#### Event: 'login' |
| 58 | + |
| 59 | +Returns: |
| 60 | + |
| 61 | +* `authInfo` Object |
| 62 | + * `isProxy` Boolean |
| 63 | + * `scheme` String |
| 64 | + * `host` String |
| 65 | + * `port` Integer |
| 66 | + * `realm` String |
| 67 | +* `callback` Function |
| 68 | + |
| 69 | +Emitted when an authenticating proxy is asking for user credentials. |
| 70 | + |
| 71 | +The `callback` function is expected to be called back with user credentials: |
| 72 | + |
| 73 | +* `username` String |
| 74 | +* `password` String |
| 75 | + |
| 76 | +```JavaScript |
| 77 | +request.on('login', (authInfo, callback) => { |
| 78 | + callback('username', 'password') |
| 79 | +}) |
| 80 | +``` |
| 81 | +Providing empty credentials will cancel the request and report an authentication |
| 82 | +error on the response object: |
| 83 | + |
| 84 | +```JavaScript |
| 85 | +request.on('response', (response) => { |
| 86 | + console.log(`STATUS: ${response.statusCode}`); |
| 87 | + response.on('error', (error) => { |
| 88 | + console.log(`ERROR: ${JSON.stringify(error)}`) |
| 89 | + }) |
| 90 | +}) |
| 91 | +request.on('login', (authInfo, callback) => { |
| 92 | + callback() |
| 93 | +}) |
| 94 | +``` |
| 95 | + |
| 96 | +#### Event: 'finish' |
| 97 | + |
| 98 | +Emitted just after the last chunk of the `request`'s data has been written into |
| 99 | +the `request` object. |
| 100 | + |
| 101 | +#### Event: 'abort' |
| 102 | + |
| 103 | +Emitted when the `request` is aborted. The `abort` event will not be fired if |
| 104 | +the `request` is already closed. |
| 105 | + |
| 106 | +#### Event: 'error' |
| 107 | + |
| 108 | +Returns: |
| 109 | + |
| 110 | +* `error` Error - an error object providing some information about the failure. |
| 111 | + |
| 112 | +Emitted when the `net` module fails to issue a network request. Typically when |
| 113 | +the `request` object emits an `error` event, a `close` event will subsequently |
| 114 | +follow and no response object will be provided. |
| 115 | + |
| 116 | +#### Event: 'close' |
| 117 | + |
| 118 | +Emitted as the last event in the HTTP request-response transaction. The `close` |
| 119 | +event indicates that no more events will be emitted on either the `request` or |
| 120 | +`response` objects. |
| 121 | + |
| 122 | +### Instance Properties |
| 123 | + |
| 124 | +#### `request.chunkedEncoding` |
| 125 | + |
| 126 | +A Boolean specifying whether the request will use HTTP chunked transfer encoding |
| 127 | +or not. Defaults to false. The property is readable and writable, however it can |
| 128 | +be set only before the first write operation as the HTTP headers are not yet put |
| 129 | +on the wire. Trying to set the `chunkedEncoding` property after the first write |
| 130 | +will throw an error. |
| 131 | + |
| 132 | +Using chunked encoding is strongly recommended if you need to send a large |
| 133 | +request body as data will be streamed in small chunks instead of being |
| 134 | +internally buffered inside Electron process memory. |
| 135 | + |
| 136 | +### Instance Methods |
| 137 | + |
| 138 | +#### `request.setHeader(name, value)` |
| 139 | + |
| 140 | +* `name` String - An extra HTTP header name. |
| 141 | +* `value` String - An extra HTTP header value. |
| 142 | + |
| 143 | +Adds an extra HTTP header. The header name will issued as it is without |
| 144 | +lowercasing. It can be called only before first write. Calling this method after |
| 145 | +the first write will throw an error. |
| 146 | + |
| 147 | +#### `request.getHeader(name)` |
| 148 | + |
| 149 | +* `name` String - Specify an extra header name. |
| 150 | + |
| 151 | +Returns String - The value of a previously set extra header name. |
| 152 | + |
| 153 | +#### `request.removeHeader(name)` |
| 154 | + |
| 155 | +* `name` String - Specify an extra header name. |
| 156 | + |
| 157 | +Removes a previously set extra header name. This method can be called only |
| 158 | +before first write. Trying to call it after the first write will throw an error. |
| 159 | + |
| 160 | +#### `request.write(chunk[, encoding][, callback])` |
| 161 | + |
| 162 | +* `chunk` (String | Buffer) - A chunk of the request body's data. If it is a |
| 163 | +string, it is converted into a Buffer using the specified encoding. |
| 164 | +* `encoding` String (optional) - Used to convert string chunks into Buffer |
| 165 | +objects. Defaults to 'utf-8'. |
| 166 | +* `callback` Function (optional) - Called after the write operation ends. |
| 167 | + |
| 168 | +`callback` is essentially a dummy function introduced in the purpose of keeping |
| 169 | +similarity with the Node.js API. It is called asynchronously in the next tick |
| 170 | +after `chunk` content have been delivered to the Chromium networking layer. |
| 171 | +Contrary to the Node.js implementation, it is not guaranteed that `chunk` |
| 172 | +content have been flushed on the wire before `callback` is called. |
| 173 | + |
| 174 | +Adds a chunk of data to the request body. The first write operation may cause |
| 175 | +the request headers to be issued on the wire. After the first write operation, |
| 176 | +it is not allowed to add or remove a custom header. |
| 177 | + |
| 178 | +#### `request.end([chunk][, encoding][, callback])` |
| 179 | + |
| 180 | +* `chunk` (String | Buffer) (optional) |
| 181 | +* `encoding` String (optional) |
| 182 | +* `callback` Function (optional) |
| 183 | + |
| 184 | +Sends the last chunk of the request data. Subsequent write or end operations |
| 185 | +will not be allowed. The `finish` event is emitted just after the end operation. |
| 186 | + |
| 187 | +#### `request.abort()` |
| 188 | + |
| 189 | +Cancels an ongoing HTTP transaction. If the request has already emitted the |
| 190 | +`close` event, the abort operation will have no effect. Otherwise an ongoing |
| 191 | +event will emit `abort` and `close` events. Additionally, if there is an ongoing |
| 192 | +response object,it will emit the `aborted` event. |
0 commit comments