HttpQuery
Sends an HTTP request.
HttpQuery(url)
HttpQuery(url, options)Examples
-
Example of accessing the OKX public market data API.
javascriptfunction main(){ // Example of a GET request without parameters var info = JSON.parse(HttpQuery("https://www.okx.com/api/v5/public/time")) Log(info) // Example of a GET request with parameters var ticker = JSON.parse(HttpQuery("https://www.okx.com/api/v5/market/books?instId=BTC-USDT")) Log(ticker) }pythonimport json def main(): # A GET request without parameters info = json.loads(HttpQuery("https://www.okx.com/api/v5/public/time")) Log(info) # A GET request with parameters ticker = json.loads(HttpQuery("https://www.okx.com/api/v5/market/books?instId=BTC-USDT")) Log(ticker)rustfn main() { // Example of a GET request without parameters. In Rust, the type annotation of the return value determines whether the Body string (String) or the complete response (HttpRet) is returned let body: String = HttpQuery("https://www.okx.com/api/v5/public/time", None); let info = JSONParse(&body).unwrap(); Log!(info); // Example of a GET request with parameters let body2: String = HttpQuery("https://www.okx.com/api/v5/market/books?instId=BTC-USDT", None); let ticker = JSONParse(&body2).unwrap(); Log!(ticker); } -
Example of using proxy settings with the HttpQuery function.
javascriptfunction main() { // Set a proxy for this call and send the HTTP request without a username and password; this HTTP request will be sent through the proxy HttpQuery("socks5://127.0.0.1:8889/http://www.baidu.com/") // Set a proxy for this call and send the HTTP request with a username and password; the proxy setting only takes effect for the current HttpQuery call, and the proxy will not be used when HttpQuery("http://www.baidu.com") is called again afterwards HttpQuery("socks5://username:[email protected]:8889/http://www.baidu.com/") // A proxy for this request can also be set with the proxy field of the options parameter HttpQuery("http://www.baidu.com/", {proxy: "socks5://username:[email protected]:8889"}) }pythondef main(): # Set a proxy for this call and send the HTTP request without a username and password; this HTTP request will be sent through the proxy HttpQuery("socks5://127.0.0.1:8889/http://www.baidu.com/") # Set a proxy for this call and send the HTTP request with a username and password; the proxy setting only takes effect for the current HttpQuery call HttpQuery("socks5://username:[email protected]:8889/http://www.baidu.com/") # A proxy for this request can also be set with the proxy field of the options parameter HttpQuery("http://www.baidu.com/", {"proxy": "socks5://username:[email protected]:8889"})rustfn main() { // Set a proxy for this call and send the HTTP request without a username and password; this HTTP request will be sent through the proxy let ret1: String = HttpQuery("socks5://127.0.0.1:8889/http://www.baidu.com/", None); // Set a proxy for this call and send the HTTP request with a username and password; the proxy setting only takes effect for the current HttpQuery call, and the proxy will not be used when HttpQuery("http://www.baidu.com") is called again afterwards let ret2: String = HttpQuery("socks5://username:[email protected]:8889/http://www.baidu.com/", None); }
Returns
| Type | Description |
string / object | The response: with |
Arguments
| Name | Type | Required | Description |
url | string | Yes | The URL of the HTTP request. |
options | object | No | Request settings, all optional: |
See Also
Remarks
The HttpQuery() function supports the JavaScript, Python and Rust languages. HttpQuery() is mainly used to access exchange interfaces that do not require a signature, such as public interfaces for market data.
In the backtesting system, HttpQuery() can be used to send requests (only GET requests are supported) to obtain data. During backtesting, access to at most 20 different URLs is allowed, and HttpQuery() caches the accessed data: when the same URL is accessed again, the HttpQuery() function returns the cached data directly without initiating an actual network request.
A failed request (no response received, e.g. connection refused, DNS failure, timeout, proxy failure) does not return a null value; an error with the request method and URL is logged, and the call returns an empty string when debug is false, or a structure with StatusCode 0 and an Error field (the reason) when debug is true. Responses with HTTP status 4xx or 5xx are not failures.