如何在以太坊平台上的Solidity中处理REST API响应?

问题描述

我在Ethereum平台上使用solidity语言调用REST API。我需要处理回应。 API返回true或false。您能帮忙处理答复吗?

提前谢谢! 我正在使用以下代码

导入“ github.com/oraclize/ethereum-api/provableAPI.sol”;

contract UrlRequests is usingProvable {

event LogNewProvableQuery(string description);

event LogResult(string result);
constructor()
    public
{
    provable_setProof(proofType_Android | proofStorage_IPFS);
}

function __callback(
    bytes32 _myid,string memory _result,bytes memory _proof
)
    public
{
    require(msg.sender == provable_cbAddress());
    emit LogResult(_result);
}

function request(
    string memory _query,string memory _method,string memory _url,string memory _kwargs
)
    public
    payable
{
    if (provable_getPrice("computation") > address(this).balance) {
        emit LogNewProvableQuery("Provabl   e query was NOT sent,please add some ETH to cover for the query fee");
    } else {
        emit LogNewProvableQuery("Provable query was sent,standing by for the answer...");
        provable_query("computation",[_query,_method,_url,_kwargs]
        );
    }
}
/**
 * @dev Sends a custom content-type in the header and returns the header used
 * as result. Wrap the first argument of computation ds with helper needed,* such as JSON in this case
 */
 function requestPost()
    public
    payable
{
    request("testaccaount","POST","https://xxxxtest",'{"json": { "username": "usertest","password": "passwordtest"}}');
} 

}

解决方法

provable_query 的响应被发送到 __ callback 函数。在您的情况下,它已记录并包含在交易信息中。此事件可能是后端的信号(已订阅合同事件/特定主题),因此它可以解析结果或从事件信息中解析事件。我建议使用这种方法。

如果您想以其他方式访问结果,则可以创建映射到结果的queryId,另一个可以将您的customerRequestID映射到queryId。然后,您可以使用requestID来获取结果。您可以在后端将“ true” /“ false”转换为

 mapping(bytes32 => string) public queryIdsResults;
 mapping(string  => bytes32) public requestsQueryIds;

function __callback(
    bytes32 _queryId,string memory _result,bytes memory _proof
)
    public
{
    require(msg.sender == provable_cbAddress());
    queryIdsResults[_queryId]=_result;
    emit LogResult(_result);
}
function request(
    string memory _requestID,string memory _query,string memory _method,string memory _url,string memory _kwargs
)
    public
    payable
{
    if (provable_getPrice("computation") > address(this).balance) {
        emit LogNewProvableQuery("Provabl   e query was NOT sent,please add some ETH to cover for the query fee");
    } else {
        emit LogNewProvableQuery("Provable query was sent,standing by for the answer...");
        bytes32 queryId = provable_query("computation",[_query,_method,_url,_kwargs]
        );
       requestsQueryIds[_requestID]= queryId;
    }
}
 function requestPost()
    public
    payable
{
      request("custom_request_id","testaccaount","POST","https://xxxxtest",'{"json": { "username": "usertest","password": "passwordtest"}}');
 provable_query("URL","json(https://api.pro.coinbase.com/products/ETH-USD/ticker).price");

} 
  function getResultByRequestID(string memory _requestID) public view returns ( string memory){
       bytes32 queryId =  requestsQueryIds[_requestID];
       return queryIdsResults[queryId];
   }

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...