Corda doesn't currently provide a mechanism for making an asynchronous HTTP request and then either suspending the flow while awaiting a response, or providing a callback that will be called when the response is received.
Instead, it is recommended that you make the HTTP request before initiating the flow, then pass in the response as an argument when instantiating the flow.
Sometimes, this isn't possible. For example, the HTTP request may be required by a response flow that is initiated automatically, or it may depend on the contents of a message received from a counterparty.
In this case, you can still support this kind of workflow as follows. Suppose you are writing a CorDapp for loan applications, which are represented as LoanApplicationState
s. Without an HTTP call, the responder doesn't know whether to accept the loan application or not.
Instead of creating a LoanApplicationState
directly, the flow would create an UnacceptedLoanApplicationState
, which the responder would store on their ledger. Once the flow ends, the responder can make an HTTP call outside of the flow framework. Based on the result of the HTTP call, the responder will either kick off an approve flow that creates a transaction to transform the UnacceptedLoanApplicationState
into an LoanApplicationState
, or kick off a reject flow to consume the UnacceptedLoanApplicationState
state without issuing an accepted LoanApplicationState
.
Alternatively, you could add a status field to the LoanApplicationState
, specifying whether the loan is approved or not. Initially, the loan state would have the field set to unapproved. Then, based on the result of the HTTP request, the responder would initiate one of two flows to update the LoanApplicationState
, updating it with either an approved or a rejected status.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…