electron/docs/api/incoming-message.md

105 lines
3.3 KiB
Markdown
Raw Normal View History

2016-11-10 20:25:26 +00:00
## Class: IncomingMessage
> Handle responses to HTTP/HTTPS requests.
feat: add net module to utility process (#40017) * chore: initial prototype of net api from utility process * chore: update url loader to work on both browser and utility processes * chore: add net files to utility process bundle * chore: re-add app ready check but only on main process * chore: replace browser thread dcheck's with sequence checker * refactor: move url loader from browser to common * refactor: move net-client-request.ts from browser to common * docs: add utility process to net api docs * refactor: move net module app ready check to browser only * refactor: switch import from main to common after moving to common * test: add basic net module test for utility process * refactor: switch browser pid with utility pid * refactor: move electron_api_net from browser to common * chore: add fetch to utility net module * chore: add isOnline and online to utility net module * refactor: move net spec helpers into helper file * refactor: break apart net module tests Adds two additional net module test files: `api-net-session-spec.ts` for tests that depend on a session being available (aka depend on running on the main process) and `api-net-custom-protocols-spec.ts` for custom protocol tests. This enables running `api-net-spec.ts` in the utility process. * test: add utility process mocha runner to run net module tests * docs: add utility process to net module classes * refactor: update imports in lib/utility to use electron/utility * chore: check browser context before using in main process Since the browser context supplied to the SimpleURLLoaderWrapper can now be null for use in the UtilityProcess, adding a null check for the main process before use to get a more sensible error if something goes wrong. Co-authored-by: Cheng Zhao <github@zcbenz.com> * chore: remove test debugging * chore: remove unnecessary header include * docs: add utility process net module limitations * test: run net module tests in utility process individually * refactor: clean up prior utility process net tests * chore: add resolveHost to utility process net module * chore: replace resolve host dcheck with sequence checker * test: add net module tests for net.resolveHost * docs: remove utility process limitation for resolveHost --------- Co-authored-by: deepak1556 <hop2deep@gmail.com> Co-authored-by: Cheng Zhao <github@zcbenz.com>
2024-01-04 21:20:37 +00:00
Process: [Main](../glossary.md#main-process), [Utility](../glossary.md#utility-process)<br />
_This class is not exported from the `'electron'` module. It is only available as a return value of other methods in the Electron API._
2016-11-10 20:25:26 +00:00
`IncomingMessage` implements the [Readable Stream](https://nodejs.org/api/stream.html#stream_readable_streams)
interface and is therefore an [EventEmitter][event-emitter].
2016-11-10 20:25:26 +00:00
### Instance Events
#### Event: 'data'
Returns:
* `chunk` Buffer - A chunk of response body's data.
The `data` event is the usual method of transferring response data into
applicative code.
#### Event: 'end'
Indicates that response body has ended. Must be placed before 'data' event.
2016-11-10 20:25:26 +00:00
#### Event: 'aborted'
Emitted when a request has been canceled during an ongoing HTTP transaction.
#### Event: 'error'
Returns:
`error` Error - Typically holds an error string identifying failure root cause.
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.
### Instance Properties
An `IncomingMessage` instance has the following readable properties:
#### `response.statusCode`
2017-07-24 08:29:03 +00:00
An `Integer` indicating the HTTP response status code.
2016-11-10 20:25:26 +00:00
#### `response.statusMessage`
A `string` representing the HTTP status message.
2016-11-10 20:25:26 +00:00
#### `response.headers`
A `Record<string, string | string[]>` representing the HTTP response headers. The `headers` object is
2016-11-10 20:25:26 +00:00
formatted as follows:
* All header names are lowercased.
* Duplicates of `age`, `authorization`, `content-length`, `content-type`,
`etag`, `expires`, `from`, `host`, `if-modified-since`, `if-unmodified-since`,
`last-modified`, `location`, `max-forwards`, `proxy-authorization`, `referer`,
`retry-after`, `server`, or `user-agent` are discarded.
* `set-cookie` is always an array. Duplicates are added to the array.
* For duplicate `cookie` headers, the values are joined together with '; '.
* For all other headers, the values are joined together with ', '.
2016-11-10 20:25:26 +00:00
#### `response.httpVersion`
A `string` indicating the HTTP protocol version number. Typical values are '1.0'
2016-11-10 20:25:26 +00:00
or '1.1'. Additionally `httpVersionMajor` and `httpVersionMinor` are two
Integer-valued readable properties that return respectively the HTTP major and
minor version numbers.
2016-11-25 12:17:31 +00:00
#### `response.httpVersionMajor`
2017-07-24 08:29:03 +00:00
An `Integer` indicating the HTTP protocol major version number.
2016-11-25 12:17:31 +00:00
#### `response.httpVersionMinor`
2017-07-24 08:29:03 +00:00
An `Integer` indicating the HTTP protocol minor version number.
[event-emitter]: https://nodejs.org/api/events.html#events_class_eventemitter
#### `response.rawHeaders`
A `string[]` containing the raw HTTP response headers exactly as they were
received. The keys and values are in the same list. It is not a list of
tuples. So, the even-numbered offsets are key values, and the odd-numbered
offsets are the associated values. Header names are not lowercased, and
duplicates are not merged.
```js @ts-type={response:Electron.IncomingMessage}
// Prints something like:
//
// [ 'user-agent',
// 'this is invalid because there can be only one',
// 'User-Agent',
// 'curl/7.22.0',
// 'Host',
// '127.0.0.1:8000',
// 'ACCEPT',
// '*/*' ]
console.log(response.rawHeaders)
```