Module methanol

Class MutableRequest

All Implemented Interfaces:
TaggableRequest.Builder, HttpRequest.Builder

public final class MutableRequest extends TaggableRequest implements TaggableRequest.Builder
A mutable HttpRequest that supports tags, relative URIs & setting arbitrary objects as request bodies. This class implements HttpRequest.Builder for setting request fields. Querying a field before it's been set will return its default value. Invoking toImmutableRequest() will return an immutable copy that is independent of this instance.

MutableRequest accepts an arbitrary object, referred to as payloads, as the request body. The payload is resolved into a BodyPublisher only when one is requested. Resolution is done by this request's AdapterCodec.

Additionally, this class allows setting a URI without a host or a scheme or not setting a URI at all. This is for the case when the request is used with a Methanol client that has a base URL, with which this request's URL is resolved.

MutableRequest also adds some convenience when the HttpRequest is used immediately after creation:


 client.send(
     MutableRequest
         .GET("https://www.google.com/search?q=java")
         .header("Accept", "text/html"),
     BodyHandlers.ofString());
 
It is recommended, however, to use toImmutableRequest() if the request is stored somewhere before it's sent in order to prevent accidental mutation, especially when the request is sent asynchronously.