The result returned from an HTTP request can be HTML, JSON, XML, binary, and so on. In Solidity, it is difficult and expensive to parse the result. Due to this, Oraclize provides parsing helpers to let it handle the parsing on its servers, and you get only the part of the result that you need.
To ask Oraclize to parse the result, you need to wrap the URL with one of these parsing helpers:
- xml(..) and json(..) helpers let you ask Oraclize to only return part of the JSON or XML-parsed response; for example, take a look at the following:
- In order to get the whole response back, you use the URL data source with the api.kraken.com/0/public/Ticker?pair=ETHUSD URL argument
- If all you want is the last-price field, you need to use the JSON parsing call as json(api.kraken.com/0/public/Ticker?pair=ETHUSD).result.XETHZUSD.c.0
- The html(..).xpath(..) helper is useful for HTML scraping. Just specify the XPATH you want as the xpath(..) argument; for example, take a look at the following:
- To fetch the text of a specific tweet, use html(https://twitter.com/oraclizeit/status/671316655893561344).xpath(//*[contains(@class, 'tweet-text')]/text()).
- The binary(..) helper is useful in getting binary files such as certificate files:
- To fetch only a portion of the binary file, you can use slice(offset,length); the first parameter is the offset, while the second one is the length of the slice you want back (both in bytes).
- Example: Fetch only the first 300 bytes from a binary CRL, binary(https://www.sk.ee/crls/esteid/esteid2015.crl).slice(0,300). The binary helper must be used with the slice option, and only binary files (not encoded) are accepted.
If and when the server is not responding or is unreachable, we will send you an empty response. You can test queries using http://app.Oraclize.it/home/test_query.