Class RestRequest
Obtain instances from the RestClient factory methods (e.g. RestClient.get(String)).
- Since:
- 9.2.1
-
Method Summary
Modifier and TypeMethodDescriptionSets the request body from an arbitrary Java object.Sets the request body directly from anHttpBody.bodyString(String value) Sets the request body to a raw string (UTF-8,text/plain).Adds a form field.Adds form fields (forapplication/x-www-form-urlencodedbodies).byte[]Returns request body bytes captured for debug logging (up to the configured body cap).longReturns the total request body length observed during write, or-1if unknown.Returns the terminal request exception, if any.Returns the measured request execution time.getFirstHeader(String name) Returns the first resolved request header with the given name (case-insensitive), ornullif absent.Returns the HTTP method for this request (e.g.Returns theX-Request-Idcorrelation id sent on this request.Returns the headers resolved onto the transport request.getUri()Returns the fully resolved URI for this request, ornullifrun()has not been called yet.booleanReturnstrue if a header with the given name (case-insensitive) is already set on this request.Adds a request header.Adds a request header with a lazy value.headers(HttpHeader... value) Adds request headers.interceptors(RestCallInterceptor... value) Adds per-request lifecycle interceptors.booleanReturnstrue if this request's body can be safely re-sent (retry gating).noTrace()Adds aNo-Trace: true header, asking the server to keep the stack trace of a failed call out of its log.Executes this request and opens a closeable, cancellable cursor over the response as atext/event-stream .Adds a path variable.Adds path variable substitutions for URI templates (e.g. replaces{id}in the URL).Adds a query parameter.Adds a query parameter with a lazy value.Adds query parameters.remoteUrlPolicy(boolean enforced, boolean allowPrivateUrls) Marks this request as a@Remote/@Urlcall subject to the SSRF guardrail (seeorg.apache.juneau.http.remote.RemoteUrlPolicy), and records the effectiveallowPrivateUrlsopt-in for it.Sets theX-Request-Idcorrelation id for this request (pre-call).run()Executes the request and returns the response.streamBodyEntity(RecordStreamBody value) Sets the request body to a token/record-streaming cursor body.Sets a per-call response timeout for this request.
-
Method Details
-
headers
Adds request headers.- Parameters:
value- The headers to add. Must not benull .- Returns:
- This object.
-
header
Adds a request header.- Parameters:
name- The header name. Must not benull .value- The header value. May benull to omit.- Returns:
- This object.
-
header
Adds a request header with a lazy value.- Parameters:
name- The header name. Must not benull .value- Supplier for the value. Must not benull .- Returns:
- This object.
-
noTrace
Adds aNo-Trace: true header, asking the server to keep the stack trace of a failed call out of its log.The server still logs the one-line access record; only the attached exception is omitted.
- Returns:
- This object.
-
hasHeader
Returnstrue if a header with the given name (case-insensitive) is already set on this request.Used by the next-gen remote-proxy engine to honor the precedence of a caller-supplied
@Headerparameter over an annotation-declaredaccept/contentTypeattribute.- Parameters:
name- The header name. Must not benull .- Returns:
true if the header is present.
-
queryData
Adds query parameters.- Parameters:
value- The parts to add. Must not benull .- Returns:
- This object.
-
queryData
Adds a query parameter.- Parameters:
name- The parameter name. Must not benull .value- The parameter value. May benull to omit.- Returns:
- This object.
-
queryData
Adds a query parameter with a lazy value.- Parameters:
name- The parameter name. Must not benull .value- Supplier for the value. Must not benull .- Returns:
- This object.
-
formData
Adds form fields (forapplication/x-www-form-urlencodedbodies).- Parameters:
value- The parts to add. Must not benull .- Returns:
- This object.
-
formData
Adds a form field.- Parameters:
name- The field name. Must not benull .value- The field value. May benull to omit.- Returns:
- This object.
-
pathData
Adds path variable substitutions for URI templates (e.g. replaces{id}in the URL).- Parameters:
value- The path parts to apply. Must not benull .- Returns:
- This object.
-
pathData
Adds a path variable.- Parameters:
name- The variable name (without braces). Must not benull .value- The replacement value. May benull .- Returns:
- This object.
-
body
Sets the request body directly from anHttpBody.- Parameters:
value- The body. May benull .- Returns:
- This object.
-
body
Sets the request body from an arbitrary Java object.The object is first run through the client's body converter chain. The default converters handle:
HttpBody(passthrough),InputStream,byte[], andFile. Custom converters can be registered on the builder.If no converter matches, the object is serialized with the client's explicitly-configured default serializer (
RestClient.getDefaultSerializer()) and sent as a string body using the serializer's content type. When no default serializer is configured, anIllegalStateExceptionis raised (there is no implicit JSON fallback) — configure one viaRestClient.Builder.defaultSerializer(org.apache.juneau.marshall.serializer.Serializer).- Parameters:
value- The body object. May benull to clear the body.- Returns:
- This object.
- Throws:
IOException- If a converter fails or the default serializer fails.IllegalStateException- If no converter matches and no default serializer is configured on the client.
-
streamBodyEntity
Sets the request body to a token/record-streaming cursor body.Convenience for
body(streamBody)that documents streaming intent and avoids a cast. TheRecordStreamBodywrites directly to the transport output stream duringrun(), so large payloads are streamed to the wire without being buffered in memory.Example:
client .post("/bulk-upload" ) .streamBodyEntity(RecordStreamBody.records (w -> {for (Beanb :source ())w .write(b ); })) .run();- Parameters:
value- The streaming body. Must not benull .- Returns:
- This object.
-
bodyString
Sets the request body to a raw string (UTF-8,text/plain).Unlike
body(Object), this bypasses the converter chain and sends the string as-is.- Parameters:
value- The string body. May benull to clear the body.- Returns:
- This object.
-
timeout
Sets a per-call response timeout for this request.Threaded onto the
TransportRequest. Intended as a response timeout; connect timeouts are otherwise a client-level setting.Transport support: every built-in transport honors this value, using its native per-request mechanism: the JDK
HttpRequesttimeout, an Apache HttpClient 4.5RequestConfig(socket + connect timeout), an Apache HttpClient 5 request-scoped response timeout, an OkHttp per-call read timeout, the Jetty response-header wait, and a simulated handler deadline in the mock transport. On expiry the transport throws aTransportException. When set, it overrides the transport's own default for this request only.A non-
null value must be positive; zero and negative durations are rejected (matching the JDKHttpRequest.timeoutcontract) so every transport behaves identically.- Parameters:
value- The response timeout. May benull to use the transport default.- Returns:
- This object.
- Throws:
IllegalArgumentException- Ifvalue is zero or negative.
-
remoteUrlPolicy
Marks this request as a@Remote/@Urlcall subject to the SSRF guardrail (seeorg.apache.juneau.http.remote.RemoteUrlPolicy), and records the effectiveallowPrivateUrlsopt-in for it.Called by the
@Remote-proxy invocation handler; not intended for direct use by ordinaryRestClient.get(url)-style calls, which are never policy-covered.- Parameters:
enforced-trueif this request is policy-covered.allowPrivateUrls-trueif theallowPrivateUrlsopt-in is in effect for this request.- Returns:
- This object.
-
interceptors
Adds per-request lifecycle interceptors.These are unioned with the client-level interceptors when the request runs: the client-level (builder) interceptors fire first, then these in the order added. The next-gen remote-proxy engine uses this to layer annotation-declared interface-level then method-level interceptors after the builder-configured ones.
- Parameters:
value- The interceptors to add. Must not benull .- Returns:
- This object.
-
isBodyRepeatable
Returnstrue if this request's body can be safely re-sent (retry gating).A request with no body is trivially repeatable. Otherwise the answer is delegated to the underlying body's
HttpBody.isRepeatable()(orTransportBody.isRepeatable()for a pre-converted body): buffered bodies (string/byte[]/file/serialized POJO) are repeatable; one-shot streaming bodies (InputStream/Reader/piped) are not.- Returns:
true if the body is absent or repeatable.
-
getMethod
Returns the HTTP method for this request (e.g."GET").- Returns:
- The method. Never
null .
-
getUri
Returns the fully resolved URI for this request, ornullifrun()has not been called yet.- Returns:
- The URI, possibly
null .
-
getResolvedHeaders
Returns the headers resolved onto the transport request.- Returns:
- The resolved headers in wire order.
-
getFirstHeader
Returns the first resolved request header with the given name (case-insensitive), ornullif absent.Reads from
getResolvedHeaders()— the finalized, wire-order list — not the pre-conversion header listhasHeader(String)uses internally for its dedup check.- Parameters:
name- The header name. Must not benull .- Returns:
- The first matching header, or
null if absent.
-
getCachedContent
Returns request body bytes captured for debug logging (up to the configured body cap).- Returns:
- Captured request bytes, or
null if not captured.
-
getCachedContentLength
Returns the total request body length observed during write, or-1if unknown.- Returns:
- The total body length.
-
getException
Returns the terminal request exception, if any.- Returns:
- The terminal exception, or
null .
-
getExecTime
Returns the measured request execution time.- Returns:
- The request execution time, or
null if unset.
-
requestId
Sets theX-Request-Idcorrelation id for this request (pre-call).Sets both the outgoing
X-Request-Idheader and the stored sent-id field returned bygetRequestId(). Use this to propagate a caller-owned correlation id; otherwise the built-in auto-send interceptor mints one at request time (unless disabled viaRestClient.Builder.sendRequestId(boolean)).- Parameters:
value- The correlation id. Must not benull .- Returns:
- This object.
- Since:
- 10.0.0
-
getRequestId
Returns theX-Request-Idcorrelation id sent on this request.Captured at request time (minted by the auto-send interceptor, or the caller-supplied value). Read as a stored field at debug-emit time — never via a live log-context scope.
- Returns:
- The sent correlation id, or
null if none was sent (e.g.sendRequestId(false)). - Since:
- 10.0.0
-
run
Executes the request and returns the response.The caller is responsible for closing the returned
RestResponse.- Returns:
- The response. Never
null . - Throws:
TransportException- If a network-level error occurs.RestCallException- If the response could not be processed.
-
openEventStream
Executes this request and opens a closeable, cancellable cursor over the response as atext/event-stream .This executes
run(), asserts a 2xx response status, then opens the response body as an event stream. Non-2xx responses throwRestCallExceptionbefore the body is treated as SSE.The response content type is not validated. This is intentional so callers can branch on response type (for example
application/json vstext/event-stream ) when protocols use both.Adds an
Accept: text/event-streamheader only when neither this request nor inherited client default headers already includeAccept.The caller owns the returned
SseEventReader; closing it also closes the underlyingRestResponse(seeResponseBody.asEventStream()). If opening the event stream fails, this method best-effort closes the response before propagating.- Returns:
- A cursor over the live event stream. Never
null . - Throws:
IOException- If a transport-level error occurs (typicallyTransportException), if the response could not be processed (typicallyRestCallException), or if the response has no body to open a stream over.
-