How to handle REST API response in Solidity on Ethereum platform?

723 views Asked by At

I call REST API on the Ethereum platform with Solidity language. I need to handle the response. API returns true or false. Could you please help to handle the response?

Thanks in advance! I am using the following code:

import "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"}}');
} 

}

1

There are 1 answers

0
Vladyslav Munin On

The response of provable_query is sent in to the __callback function. In your case it's logged and included to transaction information. This event can be a signal for backend (that is subscribed to the contract events / specific topic), so it can parse the result or from event information. I'd recommend to go with this approach.

If you want to have an access to the result in other way you can create mapping queryIds to result, and another one your customerRequestID to queryIds. And then you can use requestID to obtain the result. You can convert "true" / "false" on backend.

 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];
   }