HttpQuery
发送Http请求。
HttpQuery(url)
HttpQuery(url, options)示例
-
访问OKX公共行情API接口的例子。
javascriptfunction main(){ // 一个GET访问不带参数的例子 var info = JSON.parse(HttpQuery("https://www.okx.com/api/v5/public/time")) Log(info) // 一个GET访问带参数的例子 var ticker = JSON.parse(HttpQuery("https://www.okx.com/api/v5/market/books?instId=BTC-USDT")) Log(ticker) }pythonimport json def main(): # 一个GET访问不带参数的例子 info = json.loads(HttpQuery("https://www.okx.com/api/v5/public/time")) Log(info) # 一个GET访问带参数的例子 ticker = json.loads(HttpQuery("https://www.okx.com/api/v5/market/books?instId=BTC-USDT")) Log(ticker)rustfn main() { // 一个GET访问不带参数的例子,Rust 中由返回值类型注解决定返回 Body 字符串(String)或者完整应答(HttpRet) let body: String = HttpQuery("https://www.okx.com/api/v5/public/time", None); let info = JSONParse(&body).unwrap(); Log!(info); // 一个GET访问带参数的例子 let body2: String = HttpQuery("https://www.okx.com/api/v5/market/books?instId=BTC-USDT", None); let ticker = JSONParse(&body2).unwrap(); Log!(ticker); } -
HttpQuery函数使用代理设置。
javascriptfunction main() { // 本次设置代理并发送http请求,无用户名,无密码,此次http请求会通过代理发送 HttpQuery("socks5://127.0.0.1:8889/http://www.baidu.com/") // 本次设置代理并发送http请求,输入用户名和密码,仅HttpQuery当前调用生效,之后再次调用HttpQuery("http://www.baidu.com")这样不会使用代理 HttpQuery("socks5://username:[email protected]:8889/http://www.baidu.com/") // 也可以在options参数中使用proxy字段为本次请求设置代理 HttpQuery("http://www.baidu.com/", {proxy: "socks5://username:[email protected]:8889"}) }pythondef main(): # 本次设置代理并发送http请求,无用户名,无密码,此次http请求会通过代理发送 HttpQuery("socks5://127.0.0.1:8889/http://www.baidu.com/") # 本次设置代理并发送http请求,输入用户名和密码,仅HttpQuery当前调用生效 HttpQuery("socks5://username:[email protected]:8889/http://www.baidu.com/") # 也可以在options参数中使用proxy字段为本次请求设置代理 HttpQuery("http://www.baidu.com/", {"proxy": "socks5://username:[email protected]:8889"})rustfn main() { // 本次设置代理并发送http请求,无用户名,无密码,此次http请求会通过代理发送 let ret1: String = HttpQuery("socks5://127.0.0.1:8889/http://www.baidu.com/", None); // 本次设置代理并发送http请求,输入用户名和密码,仅HttpQuery当前调用生效,之后再次调用HttpQuery("http://www.baidu.com")这样不会使用代理 let ret2: String = HttpQuery("socks5://username:[email protected]:8889/http://www.baidu.com/", None); }
返回值
| 类型 | 描述 |
string / object | 应答数据: |
参数
| 名称 | 类型 | 必填 | 描述 |
url | string | 是 | Http请求url。 |
options | object | 否 | 请求设置,所有字段都可选: |
参考
备注
HttpQuery()函数支持JavaScript、Python、Rust语言。HttpQuery()主要用于访问交易所不需要签名的接口,例如行情信息等公共接口。
回测系统中可以使用HttpQuery()发送请求(只支持GET请求)获取数据。回测时限制使用20次访问不同的URL,并且HttpQuery()访问会缓存数据,相同的URL第二次访问时HttpQuery()函数返回缓存数据,不再发生实际网络请求。
请求失败(没有收到应答,例如拒绝连接、DNS解析失败、超时、代理失败)时不返回空值,而是在日志中记录包含请求方法和URL的错误信息:debug为false时返回空字符串;debug为true时返回StatusCode为0、带Error字段(失败原因)的结构。HTTP状态码为4xx、5xx的应答不算请求失败。