@haxall/haxall
    Preparing search index...

    Class WebClient

    The WebClient class is used to manage client side HTTP requests and responses. The basic lifecycle of WebClient:

    1. configure request fields such as reqUri, reqMethod, and reqHeaders
    2. send request headers via writeReq
    3. optionally write request body via reqOut
    4. read response status and headers via readRes
    5. process response fields such as resCode and resHeaders
    6. optionally read response body via resIn

    Using the low level methods writeReq and readRes enables HTTP pipelining (multiple requests and responses on the same TCP socket connection). There are also a series of convenience methods which make common cases easier.

    See pod doc and examples.

    Hierarchy (View Summary)

    Index

    Constructors

    Properties

    type$: Type

    Methods

    • Authenticate request using HTTP Basic with given username and password.

      Parameters

      • username: string
      • password: string

      Returns this

    • Close the HTTP request and the underlying socket. Return this.

      Returns this

    • Return a negative integer, zero, or a positive integer if this object is less than, equal to, or greater than the specified object:

      this < that   =>  <0
      this == that => 0
      this > that => >0

      This method may also be accessed via the < <= <=> >= and > shortcut operators. If not overridden the default implementation compares the toStr representations. Also see docLang.

      Examples:

      3.compare(8)  =>  -1
      8.compare(3) => 1
      8.compare(8) => 0
      3 <=> 8 => -1 // shortcut for 3.compare(8)

      Parameters

      Returns number

    • Cookies to pass for "Cookie" request header. If set to an empty list then the "Cookie" request header is removed. After a request has been completed if the "Set-Cookie" response header specified one or more cookies then this field is automatically updated with the server specified cookies.

      Returns List<Cookie>

    • Cookies to pass for "Cookie" request header. If set to an empty list then the "Cookie" request header is removed. After a request has been completed if the "Set-Cookie" response header specified one or more cookies then this field is automatically updated with the server specified cookies.

      Parameters

      Returns void

    • Compare this object to the specified for equality. This method may be accessed via the == and != shortcut operators. If not overridden the default implementation compares for reference equality using the === operator. If this method is overridden, then hash() must also be overridden such that any two objects which return true for equals() must return the same value for hash(). This method must accept null and return false.

      Parameters

      Returns boolean

    • When set to true a 3xx response with a Location header will automatically update the reqUri field and retry the request using the alternate URI. Redirects are not followed if the request has a content body.

      Returns boolean

    • When set to true a 3xx response with a Location header will automatically update the reqUri field and retry the request using the alternate URI. Redirects are not followed if the request has a content body.

      Parameters

      • it: boolean

      Returns void

    • Make a GET request and return the response content as an in-memory byte buffer. The web client is automatically closed. Throw IOErr is response is not 200.

      Returns Buf

    • Make a GET request and return the input stream to the response or throw IOErr if response is not 200. It is the caller's responsibility to close this web client.

      Returns InStream

    • Make a GET request and return the response content as an in-memory string. The web client is automatically closed. Throw IOErr is response is not 200.

      Returns string

    • Return a unique hashcode for this object. If a class overrides hash() then it must ensure if equals() returns true for any two objects then they have same hash code.

      Returns number

    • Return if this web client is currently connected to the remote host.

      Returns boolean

    • Return if this Obj is immutable and safe to share between threads:

      • an instance of a const class
      • the result of toImmutable on List, Map, or Buf
      • a Func object may or may not be immutable - see sys::Func.
      • other instances are assumed mutable and return false

      Returns boolean

    • Convenience for writeBuf("POST", content).readRes

      Parameters

      Returns this

    • Convenience for writeFile("POST", file).readRes

      Parameters

      Returns this

    • Convenience for writeForm("POST", form).readRes

      Parameters

      • form: Map<string, string>

      Returns this

    • Convenience for writeStr("POST", content).readRes

      Parameters

      • content: string

      Returns this

    • If non-null, then all requests are routed through this proxy address (host and port). Default is configured in "etc/web/config.props" with the key "proxy". Proxy exceptions can be configured with the "proxy.exceptions" config key as comma separated list of Regex globs.

      Returns Uri

    • If non-null, then all requests are routed through this proxy address (host and port). Default is configured in "etc/web/config.props" with the key "proxy". Proxy exceptions can be configured with the "proxy.exceptions" config key as comma separated list of Regex globs.

      Parameters

      Returns void

    • Read the response status line and response headers. This method may be called after the request has been written via writeReq and reqOut. Once this method completes the response status and headers are available. If there is a response body, it is available for reading via resIn. Throw IOErr if there is a network or protocol error. Return this.

      Returns this

    • The HTTP headers to use for the next request. This map uses case insensitive keys. The "Host" header is implicitly defined by reqUri and must not be defined in this map.

      Returns Map<string, string>

    • The HTTP headers to use for the next request. This map uses case insensitive keys. The "Host" header is implicitly defined by reqUri and must not be defined in this map.

      Parameters

      • it: Map<string, string>

      Returns void

    • The HTTP method for the request. Defaults to "GET".

      Returns string

    • The HTTP method for the request. Defaults to "GET".

      Parameters

      • it: string

      Returns void

    • Get the output stream used to write the request body. This stream is only available if the request headers included a "Content-Type" header. If an explicit "Content-Length" was specified then this is a fixed length output stream, otherwise the request is automatically configured to use a chunked transfer encoding. This stream should be closed once the content has been fully written.

      Returns OutStream

    • The absolute URI of request.

      Returns Uri

    • The absolute URI of request.

      Parameters

      Returns void

    • HTTP version to use for request must be 1.0 or 1.1. Default is 1.1.

      Returns Version

    • HTTP version to use for request must be 1.0 or 1.1. Default is 1.1.

      Parameters

      Returns void

    • Return the entire response back as an in-memory byte buffer. Convenience for resIn.readAllBuf.

      Returns Buf

    • HTTP status code returned by response.

      Returns number

    • HTTP status code returned by response.

      Parameters

      • it: number

      Returns void

    • Get a response header. If not found and checked is false then return true, otherwise throw Err.

      Parameters

      • key: string
      • Optionalchecked: boolean

      Returns string

    • HTTP headers returned by response.

      Returns Map<string, string>

    • HTTP headers returned by response.

      Parameters

      • it: Map<string, string>

      Returns void

    • Input stream to read response content. The input stream will correctly handle end of stream when the content has been fully read. If the "Content-Length" header was specified the end of stream is based on the fixed number of bytes. If the "Transfer-Encoding" header defines a chunked encoding, then chunks are automatically handled. If the response has no content body, then throw IOErr.

      The response input stream is automatically configured with the correct character encoding if one is specified in the "Content-Type" response header.

      Also see convenience methods: resStr and resBuf.

      Returns InStream

    • HTTP status reason phrase returned by response.

      Returns string

    • HTTP status reason phrase returned by response.

      Parameters

      • it: string

      Returns void

    • Return the entire response back as an in-memory string. Convenience for resIn.readAllStr.

      Returns string

    • HTTP version returned by response.

      Returns Version

    • HTTP version returned by response.

      Parameters

      Returns void

    • Get an immutable representation of this instance or throw NotImmutableErr if this object cannot be represented as an immutable:

      • if type is const, return this
      • if already an immutable List, Map, Buf, or Func return this
      • if a List, then attempt to perform a deep clone by calling toImmutable on all items
      • if a Map, then attempt to perform a deep clone by calling toImmutable on all values (keys are already immutable)
      • some Funcs can be made immutable - see sys::Func
      • if a Buf create immutable copy, see sys::Buf
      • any other object throws NotImmutableErr

      Returns Readonly<this>

    • Return a string representation of this object.

      Returns string

    • Trap a dynamic call for handling. Dynamic calls are invoked with the -> shortcut operator:

      a->x        a.trap("x", null)
      a->x() a.trap("x", null)
      a->x = b a.trap("x", [b])
      a->x(b) a.trap("x", [b])
      a->x(b, c) a.trap("x", [b, c])

      The default implementation provided by Obj attempts to use reflection. If name maps to a method, it is invoked with the specified arguments. If name maps to a field and args.size is zero, get the field. If name maps to a field and args.size is one, set the field and return args[0]. Otherwise throw UnknownSlotErr.

      Parameters

      Returns JsObj

    • Get the Type instance which represents this object's class. Also seeType.of or Pod.of.

      Returns Type

    • This method called whenever an it-block is applied to an object. The default implementation calls the function with this, and then returns this.

      Parameters

      • f: (arg0: this) => void

      Returns this

    • Write a binary buffer using the given HTTP method to the URI. If Content-Type header is not already set, then it is set as ""application/octet-stream". This method does not support the "Expect" header

      Parameters

      • method: string
      • content: Buf

      Returns this

    • Write a file using the given HTTP method to the URI. If Content-Type header is not already set, then it is set from the file extension's MIME type. This method does not support the "Expect" header (it writes full file before reading response). Should primarily be used for "POST" and "PATCH" requests.

      Parameters

      • method: string
      • file: File

      Returns this

    • Make a request with the given HTTP method to the URI with the given form data. Set the Content-Type to application/x-www-form-urlencoded. This method does not support the "Expect" header (it writes all form data before reading response). Should primarily be used for POST and PATCH requests.

      Parameters

      • method: string
      • form: Map<string, string>

      Returns this

    • Write the request line and request headers. Once this method completes the request body may be written via reqOut, or the response may be immediately read via readRes. Throw IOErr if there is a network or protocol error. Return this.

      Returns this

    • Make a request with the given HTTP method to the URI using UTF-8 encoding of given string. If Content-Type is not already set, then set it to "text/plain; charset=utf-8". This method does not support the "Expect" header (it writes full string before reading response). Should primarily be used for "POST" and "PATCH" requests.

      Parameters

      • method: string
      • content: string

      Returns this

    • Write x.toStr to standard output followed by newline. If x is null then print "null". If no argument is provided then print an empty line.

      Parameters

      Returns void

    • Construct with optional request URI.

      Parameters

      • OptionalreqUri: Uri
      • ...args: unknown[]

      Returns WebClient