-
Notifications
You must be signed in to change notification settings - Fork 315
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
35 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,43 +1,44 @@ | ||
# Body callbacks | ||
# Request body progress callback | ||
|
||
When using the `HttpClient`-based backends (which includes the `DefaultSyncBackend` and `DefaultFutureBackend` on the | ||
JVM), it is possible to register body-related callbacks. | ||
JVM), it is possible to register a callback that keeps track of the progress of sending the request body. | ||
|
||
This feature is not available in other backends, and setting the attribute described below will have no effect. | ||
|
||
## Request body callbacks | ||
The callback is defined through an instance of the `RequestBodyProgressCallback` trait. | ||
|
||
Defines a callback to be invoked when subsequent parts of the request body to be sent are created, just before they | ||
are sent over the network. The callback is defined through an instance of the `RequestBodyCallback` trait. | ||
|
||
When a request is sent, the `RequestBodyCallback.onInit` method is invoked exactly once with the content length (if it | ||
When a request is sent, the `RequestBodyProgressCallback.onInit` method is invoked exactly once with the content length (if it | ||
is known). This is followed by arbitrary number of `onNext` calls. Finally, either `onComplete` or `onError` are called | ||
exactly once. | ||
|
||
All of the methods in the `RequestBodyCallback` implementation should be non-blocking and complete as fast as possible, | ||
```{note} | ||
`onNext` is called when a part of the request body is ready to be sent over the network, that is, before it is actually | ||
being transferred. | ||
``` | ||
|
||
All of the methods in the `RequestBodyProgressCallback` implementation should be non-blocking and complete as fast as possible, | ||
so as not to obstruct sending data over the network. | ||
|
||
To register a callback, set the `RequestBodyCallback.Attribute` on a request. For example: | ||
To register a callback, set the `RequestBodyProgressCallback.Attribute` on a request. For example: | ||
|
||
```scala mdoc:compile-only | ||
import sttp.client4.* | ||
import sttp.client4.httpclient.{HttpClientSyncBackend, RequestBodyCallback} | ||
import java.nio.ByteBuffer | ||
import sttp.client4.httpclient.{HttpClientSyncBackend, RequestBodyProgressCallback} | ||
import java.io.File | ||
|
||
val backend = HttpClientSyncBackend() | ||
|
||
val fileToSend: File = ??? | ||
val callback = new RequestBodyCallback { | ||
val callback = new RequestBodyProgressCallback { | ||
override def onInit(contentLength: Option[Long]): Unit = println(s"expected content length: $contentLength") | ||
override def onNext(b: ByteBuffer): Unit = println(s"next, bytes: ${b.remaining()}") | ||
override def onNext(bytesCount: Long): Unit = println(s"next, bytes: $bytesCount") | ||
override def onComplete(): Unit = println(s"complete") | ||
override def onError(t: Throwable): Unit = println(s"error: ${t.getMessage}") | ||
} | ||
|
||
val response = basicRequest | ||
.get(uri"http://example.com") | ||
.body(fileToSend) | ||
.attribute(RequestBodyCallback.Attribute, callback) | ||
.attribute(RequestBodyProgressCallback.Attribute, callback) | ||
.send(backend) | ||
``` |