Options
All
  • Public
  • Public/Protected
  • All
Menu

Class HttpClientRequest

Represents a client-side HTTP request.

Instances are created by an HttpClient instance, via one of the methods corresponding to the specific HTTP methods, or the generic request methods. On creation the request will not have been written to the wire.

Once a request has been obtained, headers can be set on it, and data can be written to its body if required. Once you are ready to send the request, one of the {@link HttpClientRequest#end} methods should be called.

Nothing is actually sent until the request has been internally assigned an HTTP connection.

The HttpClient instance will return an instance of this class immediately, even if there are no HTTP connections available in the pool. Any requests sent before a connection is assigned will be queued internally and actually sent when an HTTP connection becomes available from the pool.

The headers of the request are queued for writing either when the {@link HttpClientRequest#end} method is called, or, when the first part of the body is written, whichever occurs first.

This class supports both chunked and non-chunked HTTP.

It implements WriteStream so it can be used with Pipe to pipe data with flow control.

An example of using this class is as follows:

Hierarchy

  • HttpClientRequest

Implements

Index

Methods

absoluteURI

  • absoluteURI(): string
  • Returns string

connect

  • Create an HTTP tunnel to the server.

    Send HTTP request headers to the server, then configures the transport to exchange raw buffers when the server replies with an appropriate response:

    • 200 for HTTP CONNECT method
    • 101 for HTTP/1.1 GET with Upgrade connection header

    The handler is called after response headers are received.

    Use {@link HttpClientResponse#netSocket} to get a for interacting more conveniently with the server.

    HTTP/1.1 pipe-lined requests are not supported.f

    Returns PromiseLike<HttpClientResponse>

  • Create an HTTP tunnel to the server.

    Send HTTP request headers to the server, then configures the transport to exchange raw buffers when the server replies with an appropriate response:

    • 200 for HTTP CONNECT method
    • 101 for HTTP/1.1 GET with Upgrade connection header

    The handler is called after response headers are received.

    Use {@link HttpClientResponse#netSocket} to get a for interacting more conveniently with the server.

    HTTP/1.1 pipe-lined requests are not supported.f

    Parameters

    Returns void

connection

  • Returns HttpConnection

continueHandler

  • If you send an HTTP request with the header Expect set to the value 100-continue and the server responds with an interim HTTP response with a status code of 100 and a Continue handler has been set using this method, then the handler will be called.

    You can then continue to write data to the request body and later end it. This is normally used in conjunction with the {@link HttpClientRequest#sendHead} method to force the request header to be written before the request has ended.

    Parameters

    • handler: ((res: void) => void) | Handler<void> | null | undefined

    Returns HttpClientRequest

drainHandler

  • Parameters

    • handler: ((res: void) => void) | Handler<void> | null | undefined

    Returns HttpClientRequest

earlyHintsHandler

  • If the server responds with an interim HTTP response with a status code of 103 and a Early Hints handler has been set using this method, then the handler will be called.

    Parameters

    Returns HttpClientRequest

end

  • Same as {@link HttpClientRequest#end} but with an handler called when the operation completes

    Parameters

    • chunk: string

    Returns PromiseLike<void>

  • Same as {@link HttpClientRequest#end} but with an handler called when the operation completes

    Parameters

    Returns void

  • Same as {@link HttpClientRequest#end} but with an handler called when the operation completes

    Parameters

    • chunk: string
    • enc: string

    Returns PromiseLike<void>

  • Same as {@link HttpClientRequest#end} but with an handler called when the operation completes

    Parameters

    Returns void

  • Same as {@link HttpClientRequest#end} but with an handler called when the operation completes

    Parameters

    Returns PromiseLike<void>

  • Same as {@link HttpClientRequest#end} but with an handler called when the operation completes

    Parameters

    Returns void

  • Same as {@link HttpClientRequest#end} but with an handler called when the operation completes

    Returns PromiseLike<void>

  • Same as {@link HttpClientRequest#end} but with an handler called when the operation completes

    Parameters

    Returns void

exceptionHandler

getHost

  • getHost(): string
  • Returns string

getMethod

  • The HTTP method for the request.

    Returns HttpMethod

getPort

  • getPort(): number
  • Returns number

getStreamPriority

  • Returns StreamPriority

getURI

  • getURI(): string
  • Returns string

headers

  • Returns MultiMap

isChunked

  • isChunked(): boolean
  • Returns boolean

path

  • path(): string
  • Returns string

pushHandler

  • Set a push handler for this request.

    The handler is called when the client receives a push promise from the server. The handler can be called multiple times, for each push promise.

    The handler is called with a read-only HttpClientRequest, the following methods can be called:

    • {@link HttpClientRequest#getMethod}
    • {@link HttpClientRequest#getURI}
    • {@link HttpClientRequest#headers}

    In addition the handler should call the {@link HttpClientRequest#response} method to set an handler to process the response.

    Parameters

    Returns HttpClientRequest

putHeader

  • Put an HTTP header

    Parameters

    • name: string
    • value: string

    Returns HttpClientRequest

  • Like {@link HttpClientRequest#putHeader} but using CharSequence

    Parameters

    • name: string
    • value: string

    Returns HttpClientRequest

  • Put an HTTP header with multiple values

    Parameters

    • name: string
    • values: any

    Returns HttpClientRequest

  • Like {@link HttpClientRequest#putHeader} but using CharSequence

    Parameters

    • name: string
    • values: any

    Returns HttpClientRequest

query

  • query(): string
  • Returns string

reset

  • reset(): boolean
  • reset(code: number): boolean
  • reset(code: number, cause: Throwable | Error): boolean
  • Reset this stream with the error code 0.

    Returns boolean

  • Reset this request:

    • for HTTP/2, this performs send an HTTP/2 reset frame with the specified error code
    • for HTTP/1.x, this closes the connection when the current request is inflight

    When the request has not yet been sent, the request will be aborted and false is returned as indicator.

    Parameters

    • code: number

    Returns boolean

  • Reset this request:

    • for HTTP/2, send an HTTP/2 reset frame with the specified error code
    • for HTTP/1.x, close the connection when the current request is inflight

    When the request has not yet been sent, the request will be aborted and false is returned as indicator.

    Parameters

    Returns boolean

response

send

  • Send the request with an empty body.

    Returns PromiseLike<HttpClientResponse>

  • Send the request with an empty body.

    Parameters

    Returns void

  • Send the request with a string body.

    Parameters

    • body: string

    Returns PromiseLike<HttpClientResponse>

  • Send the request with a string body.

    Parameters

    Returns void

  • Send the request with a buffer body.

    Parameters

    Returns PromiseLike<HttpClientResponse>

  • Send the request with a buffer body.

    Parameters

    Returns void

  • Send the request with a stream body.

    If the CONTENT_LENGTH is set then the request assumes this is the length of the {stream}, otherwise the request will set a chunked CONTENT_ENCODING.

    Parameters

    Returns PromiseLike<HttpClientResponse>

  • Send the request with a stream body.

    If the CONTENT_LENGTH is set then the request assumes this is the length of the {stream}, otherwise the request will set a chunked CONTENT_ENCODING.

    Parameters

    Returns void

sendHead

  • Like {@link HttpClientRequest#sendHead} but with an handler after headers have been sent. The handler will be called with the HttpVersion if it can be determined or null otherwise.

    Returns PromiseLike<void>

  • Like {@link HttpClientRequest#sendHead} but with an handler after headers have been sent. The handler will be called with the HttpVersion if it can be determined or null otherwise.

    Parameters

    Returns HttpClientRequest

setChunked

  • If chunked is true then the request will be set into HTTP chunked mode

    Parameters

    • chunked: boolean

    Returns HttpClientRequest

setFollowRedirects

  • Set the request to follow HTTP redirects up to HttpClientOptions.

    Parameters

    • followRedirects: boolean

    Returns HttpClientRequest

setHost

  • Set the host value of the HTTP/1.1 host header or HTTP/2 authority pseudo header

    The initial value is the same than the server socket address host.

    Keep in mind that changing this value won't change the actual server socket address for this request.

    Parameters

    • host: string

    Returns HttpClientRequest

setMaxRedirects

  • Set the max number of HTTP redirects this request will follow. The default is 0 which means no redirects.

    Parameters

    • maxRedirects: number

    Returns HttpClientRequest

setMethod

  • Set the HTTP method for this request.

    Parameters

    Returns HttpClientRequest

setPort

  • Set the port value of the HTTP/1.1 host header or HTTP/2 authority pseudo header

    Keep in mind that this won't change the actual server socket address for this request.

    The initial value is the same than the server socket address port.

    Parameters

    • port: number

    Returns HttpClientRequest

setStreamPriority

  • Sets the priority of the associated stream.

    This is not implemented for HTTP/1.x.

    Parameters

    Returns HttpClientRequest

setTimeout

  • Set's the amount of time after which if the request does not return any data within the timeout period an TimeoutException will be passed to the exception handler (if provided) and the request will be closed.

    Calling this method more than once has the effect of canceling any existing timeout and starting the timeout from scratch.

    Parameters

    • timeoutMs: number

    Returns HttpClientRequest

setURI

  • Set the request uri.

    Parameters

    • uri: string

    Returns HttpClientRequest

setWriteQueueMaxSize

streamId

  • streamId(): number
  • Returns number

version

  • Returns HttpVersion

write

  • Same as but with an handler called when the operation completes

    Parameters

    Returns PromiseLike<void>

  • Same as but with an handler called when the operation completes

    Parameters

    Returns void

  • Same as {@link HttpClientRequest#write} but with an handler called when the operation completes

    Parameters

    • chunk: string

    Returns PromiseLike<void>

  • Same as {@link HttpClientRequest#write} but with an handler called when the operation completes

    Parameters

    Returns void

  • Same as {@link HttpClientRequest#write} but with an handler called when the operation completes

    Parameters

    • chunk: string
    • enc: string

    Returns PromiseLike<void>

  • Same as {@link HttpClientRequest#write} but with an handler called when the operation completes

    Parameters

    Returns void

writeCustomFrame

  • Write an HTTP/2 frame to the request, allowing to extend the HTTP/2 protocol.

    The frame is sent immediatly and is not subject to flow control.

    This method must be called after the request headers have been sent and only for the protocol HTTP/2. The {@link HttpClientRequest#sendHead} should be used for this purpose.

    Parameters

    • type: number
    • flags: number
    • payload: Buffer

    Returns HttpClientRequest

  • Like {@link HttpClientRequest#writeCustomFrame} but with an HttpFrame.

    Parameters

    Returns HttpClientRequest

writeQueueFull

  • writeQueueFull(): boolean
  • This will return true if there are more bytes in the write queue than the value set using {@link HttpClientRequest#setWriteQueueMaxSize}

    Returns boolean

Generated using TypeDoc