class

WebClient

class WebClient : Obj

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.

constructors make

Construct with optional request URI

fields socketConfig

The SocketConfig to use for creating sockets

proxy

If non-null, then all requests are routed through this proxy address (host and port)

reqHeaders

The HTTP headers to use for the next request

resVersion

HTTP version returned by response

reqUri

The absolute URI of request

reqMethod

The HTTP method for the request

followRedirects

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

resPhrase

HTTP status reason phrase returned by response

resCode

HTTP status code returned by response

reqVersion

HTTP version to use for request must be 1.0 or 1.1

resHeaders

HTTP headers returned by response

cookies

Cookies to pass for "Cookie" request header

methods getIn

Make a GET request and return the input stream to the response or throw IOErr if response is not 200

writeStr

Make a request with the given HTTP method to the URI using UTF-8 encoding of given string

postFile

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

writeBuf

Write a binary buffer using the given HTTP method to the URI

writeForm

Make a request with the given HTTP method to the URI with the given form data

socketOptions

isConnected

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

readRes

Read the response status line and response headers

writeReq

Write the request line and request headers

getStr

Make a GET request and return the response content as an in-memory string

resIn

Input stream to read response content

authBasic

Authenticate request using HTTP Basic with given username and password

getBuf

Make a GET request and return the response content as an in-memory byte buffer

resHeader

Get a response header

postForm

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

reqOut

Get the output stream used to write the request body

resStr

Return the entire response back as an in-memory string

close

Close the HTTP request and the underlying socket

postBuf

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

writeFile

Write a file using the given HTTP method to the URI

resBuf

Return the entire response back as an in-memory byte buffer

postStr

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

authBasic This authBasic(Str username, Str password)

Authenticate request using HTTP Basic with given username and password.

close This close()

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

cookies Cookie[] : cookies

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.

followRedirects Bool : followRedirects

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.

getBuf Buf getBuf()

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.

getIn InStream getIn()

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.

getStr Str getStr()

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.

isConnected Bool isConnected()

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

make new make(Uri? reqUri)

Construct with optional request URI.

postBuf This postBuf(Buf buf)

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

postFile This postFile(File file)

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

postForm This postForm(Str:Str form)

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

postStr This postStr(Str content)

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

proxy Uri? : proxy

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.

readRes This readRes()

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.

reqHeaders Str:Str : reqHeaders

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.

reqMethod Str : reqMethod

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

reqOut OutStream reqOut()

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.

reqUri Uri : reqUri

The absolute URI of request.

reqVersion Version : reqVersion

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

resBuf Buf resBuf()

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

resCode Int : resCode

HTTP status code returned by response.

resHeader Str? resHeader(Str key, Bool checked)

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

resHeaders Str:Str : resHeaders

HTTP headers returned by response.

resIn InStream resIn()

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.

resPhrase Str : resPhrase

HTTP status reason phrase returned by response.

resStr Str resStr()

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

resVersion Version : resVersion

HTTP version returned by response.

socketConfig SocketConfig : socketConfig

The SocketConfig to use for creating sockets

socketOptions @Deprecated { msg=... }
SocketOptions socketOptions()

writeBuf This writeBuf(Str method, Buf content)

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

writeFile This writeFile(Str method, File file)

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.

writeForm This writeForm(Str method, Str:Str form)

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.

writeReq This writeReq()

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.

writeStr This writeStr(Str method, Str content)

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.

Haxall 4.0.5 ∙ 24-Feb-2026 14:33 EST