Type/to search
Built-in Functions
Global
Log
Market
Trade
Account
Futures
Exchange
IO
Network
HttpQuery
HttpQuery_Go
Dial
Mail
Mail_Go
Storage
Threads
TA
Talib
OS
Structures
Built-in Variables and Constants
Conventions
Exchanges

Sends an HTTP request.

HttpQuery(url)
HttpQuery(url, options)

Examples

  • Example of accessing the OKX public market data API.

    javascript
    function 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) }
    python
    import 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)
    rust
    fn 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.

    javascript
    function 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"}) }
    python
    def 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"})
    rust
    fn 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

TypeDescription

string / object

The response: with debug false (default) the response Body string, which you parse yourself if it is JSON (JSON.parse(), json.loads(), JSONParse()); with debug true the full response object, see HttpQuery-return. See the remarks for the return value of a failed request.

Arguments

NameTypeRequiredDescription

url

string

Yes

The URL of the HTTP request.

options

object

No

Request settings, all optional: method, body (data is used as the body when body is not set), headers, cookie, timeout (milliseconds, default 300000), charset, proxy and debug; see HttpQuery-options.

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.