ClientRequest

Constructors

Link copied to clipboard
constructor(options: ClientRequestConstructorOptions)

ClientRequest

constructor(options: String)

ClientRequest

Properties

Link copied to clipboard

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.

Functions

Link copied to clipboard
fun abort()

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.

Link copied to clipboard
fun addListener(event: ClientRequestEvent.ERROR, listener: (JsError) -> Unit)
fun addListener(event: ClientRequestEvent.LOGIN, listener: (authInfo: AuthInfo, callback: (username: String?, password: String?) -> Unit) -> Unit)
fun addListener(event: ClientRequestEvent.REDIRECT, listener: (statusCode: Double, method: String, redirectUrl: String, responseHeaders: ReadonlyRecord<String, ReadonlyArray<String>>) -> Unit)
fun addListener(event: ClientRequestEvent.RESPONSE, listener: (response: IncomingMessage) -> Unit)
Link copied to clipboard
fun end()
fun end(chunk: String = definedExternally, encoding: String = definedExternally, callback: () -> Unit = definedExternally)
fun end(chunk: Buffer = definedExternally, encoding: String = definedExternally, callback: () -> Unit = definedExternally)

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.

Link copied to clipboard
Link copied to clipboard

Continues any pending redirection. Can only be called during a 'redirect' event.

Link copied to clipboard
fun getHeader(name: String): String

The value of a previously set extra header name.

Link copied to clipboard
Link copied to clipboard

You can use this method in conjunction with POST requests to get the progress of a file upload or other data transfer.

Link copied to clipboard
fun off(event: ClientRequestEvent.ABORT, listener: Function<Unit>)
fun off(event: ClientRequestEvent.CLOSE, listener: Function<Unit>)
fun off(event: ClientRequestEvent.ERROR, listener: (JsError) -> Unit)
fun off(event: ClientRequestEvent.FINISH, listener: Function<Unit>)
fun off(event: ClientRequestEvent.LOGIN, listener: (authInfo: AuthInfo, callback: (username: String?, password: String?) -> Unit) -> Unit)
fun off(event: ClientRequestEvent.REDIRECT, listener: (statusCode: Double, method: String, redirectUrl: String, responseHeaders: ReadonlyRecord<String, ReadonlyArray<String>>) -> Unit)
fun off(event: ClientRequestEvent.RESPONSE, listener: (response: IncomingMessage) -> Unit)
Link copied to clipboard
fun on(event: ClientRequestEvent.ABORT, listener: Function<Unit>)

Emitted when the request is aborted. The abort event will not be fired if the request is already closed.

fun on(event: ClientRequestEvent.CLOSE, listener: Function<Unit>)

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.

fun on(event: ClientRequestEvent.ERROR, listener: (JsError) -> Unit)

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.

fun on(event: ClientRequestEvent.FINISH, listener: Function<Unit>)

Emitted just after the last chunk of the request's data has been written into the request object.

fun on(event: ClientRequestEvent.LOGIN, listener: (authInfo: AuthInfo, callback: (username: String?, password: String?) -> Unit) -> Unit)

Emitted when an authenticating proxy is asking for user credentials.

fun on(event: ClientRequestEvent.REDIRECT, listener: (statusCode: Double, method: String, redirectUrl: String, responseHeaders: ReadonlyRecord<String, ReadonlyArray<String>>) -> Unit)

Emitted when the server returns a redirect response (e.g. 301 Moved Permanently). Calling request.followRedirect will continue with the redirection. If this event is handled, request.followRedirect must be called synchronously, otherwise the request will be cancelled.

fun on(event: ClientRequestEvent.RESPONSE, listener: (response: IncomingMessage) -> Unit)
Link copied to clipboard
fun once(event: ClientRequestEvent.ABORT, listener: Function<Unit>)
fun once(event: ClientRequestEvent.CLOSE, listener: Function<Unit>)
fun once(event: ClientRequestEvent.ERROR, listener: (JsError) -> Unit)
fun once(event: ClientRequestEvent.LOGIN, listener: (authInfo: AuthInfo, callback: (username: String?, password: String?) -> Unit) -> Unit)
fun once(event: ClientRequestEvent.REDIRECT, listener: (statusCode: Double, method: String, redirectUrl: String, responseHeaders: ReadonlyRecord<String, ReadonlyArray<String>>) -> Unit)
fun once(event: ClientRequestEvent.RESPONSE, listener: (response: IncomingMessage) -> Unit)
Link copied to clipboard
fun removeHeader(name: String)

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.

Link copied to clipboard
fun removeListener(event: ClientRequestEvent.LOGIN, listener: (authInfo: AuthInfo, callback: (username: String?, password: String?) -> Unit) -> Unit)
fun removeListener(event: ClientRequestEvent.REDIRECT, listener: (statusCode: Double, method: String, redirectUrl: String, responseHeaders: ReadonlyRecord<String, ReadonlyArray<String>>) -> Unit)
fun removeListener(event: ClientRequestEvent.RESPONSE, listener: (response: IncomingMessage) -> Unit)
Link copied to clipboard
fun setHeader(name: String, value: String)

Adds an extra HTTP header. The header name will be issued as-is without lowercasing. It can be called only before first write. Calling this method after the first write will throw an error. If the passed value is not a string, its toString() method will be called to obtain the final value.

Link copied to clipboard
Link copied to clipboard
fun write(chunk: String, encoding: String = definedExternally, callback: () -> Unit = definedExternally)
fun write(chunk: Buffer, encoding: String = definedExternally, callback: () -> Unit = definedExternally)

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.