REST APIs with HttpTask
How to use HttpTask and JsonParseTask
HttpTask + JsonParseTask
Next to Oracle and Decentralized Exchange tasks, the most popular way to fetch data is the HttpTask combined with a JsonParseTask. The HttpTask
has support for GET and POST methods, along with custom headers and a request body (both optional).
Example
This Binance api call will result in a json response of the following structurefor all tokens:
In order to pull out the price
field from the response, we must follow the HttpTask along with a JsonParseTask
. These tasks utilize JsonPaths in order to specify which data to put into the current context. In this case, since we just ran an HttpTask and received this JSON, we have that JSON set as the current_value.
Now, the user can use a JsonParseTask to specify the path of the decimal they want to extract, in this case the field BTC-USDT price
.
Binance BTC/USDT
This is the assembled tasks to form a Binance BTC/USDT task.
OKX BTC/USDT
Pulling the latest price of BTC in USDT terms from OKX. Just another common HttpTask example, this time getting a value from an array. The return type for the OKX value in this example is:
Kraken Example
Here you can see that the user who made this job is using a MedianTask
. This allows users to run multiple jobs in parallel and get the median result. Users can also specify a maxRangePercent
in the median task in order to fail the request if that value is exceeded.
For example:
Huobi Example
POST data with HttpTask
The following is an example with a POST request including headers and a body in an HttpTask:
This is a POST request to example.com. Note that body has to be an encoded JSON string if it is set at all.
Last updated