This CorDapp provides a simple example of how HTTP requests can be made in flows. In this case, the flow makes an HTTP request to retrieve the original BitCoin readme from GitHub.
Be aware that support of HTTP requests in flows is currently limited:
- The request must be executed in a BLOCKING way. Flows don't currently support suspending to await an HTTP call's response
- The request must be idempotent. If the flow fails and has to restart from a checkpoint, the request will also be replayed
Be careful when making HTTP calls in flows; they have to be blocking. In addition, if the flow fails and is restarted, the HTTP request will be replayed as-is.
You'll find our HTTP request example within HTTPCallFlow.kt
It works mostly as you'd expect, using a request builder to make a request at a client and use the result.
@Suspendable
override fun call(): String {
val httpRequest = Request.Builder().url(BITCOIN_README_URL).build()
// BE CAREFUL when making HTTP calls in flows:
// 1. The request must be executed in a BLOCKING way. Flows don't
// currently support suspending to await an HTTP call's response
// 2. The request must be idempotent. If the flow fails and has to
// restart from a checkpoint, the request will also be replayed
val httpResponse = OkHttpClient().newCall(httpRequest).execute()
return httpResponse.body().string()
}
For development environment setup, please refer to: Setup Guide.
Open a terminal and go to the project root directory and type: (to deploy the nodes using bootstrapper)
./gradlew clean deployNodes
Then type: (to run the nodes)
./build/nodes/runnodes
We'll be interacting with the node via its interactive shell.
To have the node use a flow to retrieve the HTTP of the original Bitcoin URL, run the following command in the node's shell:
start HttpCallFlow
The text of the first commit of the BitCoin readme will be printed to the terminal window.