execute

suspend fun <T> execute(block: suspend (response: HttpResponse) -> T): T(source)

Executes the HTTP statement and invokes the provided block with the streaming HttpResponse.

The response holds an open network connection until block completes. You can access the response body incrementally (streaming) or load it entirely with body<T>().

After block finishes, the response is finalized based on the engine's configuration—either discarded or released. The response object should not be accessed outside of block as it will be canceled upon block completion.

Dispatcher Behavior

On non-JVM platforms (Web, Native), the block is executed on the engine's dispatcher, making it safe to perform IO operations such as reading the response content and writing it into a file.

On JVM, the block runs on the caller's dispatcher by default for backward compatibility. To enable engine dispatcher switching on JVM, set the system property: -Dio.ktor.client.statement.useEngineDispatcher=true

Note: Starting from Ktor 4.0, dispatcher switching will be enabled by default on all platforms. It is recommended to opt-in early to ensure compatibility with the upcoming release.

Report a problem

Return

The result of executing block with the streaming response.

Parameters

block

A suspend function that receives the HttpResponse for streaming.


suspend fun execute(): HttpResponse(source)

Executes the HTTP statement and returns the full HttpResponse.

Once the method completes, the response body is downloaded fully into memory, and the connection is released. This is suitable for requests where the entire response body is needed at once.

For retrieving a specific data type directly, consider using body().

Report a problem

Return

HttpResponse The complete response with the body loaded into memory.