Module java.httpclient
Package java.net.http

## Interface HttpRequest.BodyProcessor

Enclosing class:
  HttpRequest

    public static interface HttpRequest.BodyProcessor

A request processor which obtains the request body from some source. Implementations of this interface are provided which allow request bodies to be supplied from standard types, such as `String, byte[], File, InputStream`. Other implementations can be provided.

The methods of this interface may be called from multiple threads, but only one method is invoked at a time, and behaves as if called from one thread.

See `HttpRequest` for implementations that take request bodies from `byte arrays, Strings, Paths` etc.

Since:
  9

### Method Summary

All Methods Instance Methods Abstract Methods Default Methods
`default void` `onComplete()` Called when the request body has been completely sent.
`boolean` `onRequestBodyChunk(ByteBuffer buffer)` Called to obtain a buffer of data to send.
`default void` `onRequestError(Throwable t)` Called if sending a request body fails.
`long` `onRequestStart(HttpRequest hr, LongConsumer flowController)` Called before a request is sent.

### Method Detail

#### onRequestStart

    long onRequestStart(HttpRequest hr,
                        LongConsumer flowController)
                 throws IOException

Called before a request is sent. Is expected to return the content length of the request body. Zero means no content. Less than zero means an unknown positive content-length, and the body will be streamed.

The flowController object must be used to manage the flow of calls to `onRequestBodyChunk(ByteBuffer)`. The typical usage for a non-blocking processor is to call it once inside onRequestStart() and once during each call to onRequestBodyChunk().

Parameters:
  `hr` - the request
  `flowController` - the HttpFlowController
Returns:
  the content length
Throws:
  `IOException` - if an I/O error occurs

#### onRequestError

    default void onRequestError(Throwable t)

Called if sending a request body fails.

Implementation Requirements:
  The default implementation does nothing.
Parameters:
  `t` - the Throwable that caused the failure

#### onRequestBodyChunk

    boolean onRequestBodyChunk(ByteBuffer buffer)
                        throws IOException

Called to obtain a buffer of data to send. The data must be placed in the provided buffer. The implementation should not block. The boolean return code notifies the protocol implementation if the supplied buffer is the final one (or not).

Parameters:
  `buffer` - a ByteBuffer to write data into
Returns:
  whether or not this is the last buffer
Throws:
  `IOException` - if an I/O error occurs

#### onComplete

    default void onComplete()

Called when the request body has been completely sent.

Implementation Requirements:
  The default implementation does nothing