欢迎使用全新的 WEEX API 文档。需要查看旧版本?切换到旧版文档
WEEX
快速开始

请求交互

package com.weex.utils; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpPost; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import org.apache.http.util.EntityUtils; import javax.crypto.Mac; import javax.crypto.spec.SecretKeySpec; import java.nio.charset.StandardCharsets; import java.util.Base64; public class ApiClient { // API信息 private static final String API_KEY = ""; // 替换为实际的 API Key private static final String SECRET_KEY = ""; // 替换为实际的 Secret Key private static final String ACCESS_PASSPHRASE = ""; // 替换为实际的 Access Passphrase private static final String BASE_URL = "https://api-spot.weex.com"; // 替换为实际的 API 地址 // 生成签名(POST请求) public static String generateSignature(String secretKey, String timestamp, String method, String requestPath, String queryString, String body) throws Exception { String message = timestamp + method.toUpperCase() + requestPath + queryString + body; return generateHmacSha256Signature(secretKey, message); } // 生成签名(GET请求) public static String generateSignatureGet(String secretKey, String timestamp, String method, String requestPath, String queryString) throws Exception { String message = timestamp + method.toUpperCase() + requestPath + queryString; return generateHmacSha256Signature(secretKey, message); } // 生成 HMAC SHA256 签名 private static String generateHmacSha256Signature(String secretKey, String message) throws Exception { SecretKeySpec secretKeySpec = new SecretKeySpec(secretKey.getBytes(StandardCharsets.UTF_8), "HmacSHA256"); Mac mac = Mac.getInstance("HmacSHA256"); mac.init(secretKeySpec); byte[] signatureBytes = mac.doFinal(message.getBytes(StandardCharsets.UTF_8)); return Base64.getEncoder().encodeToString(signatureBytes); } // 发送 POST 请求 public static String sendRequestPost(String apiKey, String secretKey, String accessPassphrase, String method, String requestPath, String queryString, String body) throws Exception { String timestamp = String.valueOf(System.currentTimeMillis()); String signature = generateSignature(secretKey, timestamp, method, requestPath, queryString, body); HttpPost postRequest = new HttpPost(BASE_URL + requestPath); postRequest.setHeader("ACCESS-KEY", apiKey); postRequest.setHeader("ACCESS-SIGN", signature); postRequest.setHeader("ACCESS-TIMESTAMP", timestamp); postRequest.setHeader("ACCESS-PASSPHRASE", accessPassphrase); postRequest.setHeader("Content-Type", "application/json"); StringEntity entity = new StringEntity(body, StandardCharsets.UTF_8); postRequest.setEntity(entity); try (CloseableHttpClient httpClient = HttpClients.createDefault()) { CloseableHttpResponse response = httpClient.execute(postRequest); return EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8); } } // 发送 GET 请求 public static String sendRequestGet(String apiKey, String secretKey, String accessPassphrase, String method, String requestPath, String queryString) throws Exception { String timestamp = String.valueOf(System.currentTimeMillis()); String signature = generateSignatureGet(secretKey, timestamp, method, requestPath, queryString); HttpGet getRequest = new HttpGet(BASE_URL + requestPath+queryString); getRequest.setHeader("ACCESS-KEY", apiKey); getRequest.setHeader("ACCESS-SIGN", signature); getRequest.setHeader("ACCESS-TIMESTAMP", timestamp); getRequest.setHeader("ACCESS-PASSPHRASE", accessPassphrase); getRequest.setHeader("Content-Type", "application/json"); try (CloseableHttpClient httpClient = HttpClients.createDefault()) { CloseableHttpResponse response = httpClient.execute(getRequest); return EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8); } } // 示例调用 public static void main(String[] args) { try { // GET 请求示例 String requestPath = "/api/v3/openOrders"; String queryString = "?symbol=BTCUSDT"; String response = sendRequestGet(API_KEY, SECRET_KEY, ACCESS_PASSPHRASE, "GET", requestPath, queryString); System.out.println("GET Response: " + response); // POST 请求示例 String postPath = "/api/v3/order"; String body = "{\"symbol\":\"BTCUSDT\",\"side\":\"BUY\",\"type\":\"LIMIT\",\"timeInForce\":\"GTC\",\"quantity\":\"0.01\",\"price\":\"68900\"}"; response = sendRequestPost(API_KEY, SECRET_KEY, ACCESS_PASSPHRASE, "POST", postPath, "", body); System.out.println("POST Response: " + response); } catch (Exception e) { e.printStackTrace(); } } }

所有请求基于Https协议,请求头信息中Content-Type 需要统一设置为: 'application/json'。

请求交互说明
  • 请求参数:根据接口请求参数规定进行参数封装。
  • 提交请求参数:将封装好的请求参数通过GET/POST方式提交至服务器。
  • 服务器响应:服务器首先对用户请求数据进行参数安全校验,通过校验后根据业务逻辑将响应数据以JSON格式返回给用户。
  • 数据处理:对服务器响应数据进行处理。
成功

HTTP状态码200表示成功响应,并可能包含内容。如果响应含有内容,则将显示在相应的返回内容里面。

常见错误码
  • 400 Bad Request – Invalid request format 请求格式无效
  • 401 Unauthorized – Invalid API Key 无效的API Key
  • 403 Forbidden – You do not have access to the requested resource 请求无权限
  • 404 Not Found 没有找到请求
  • 429 Too Many Requests 请求太频繁被系统限流
  • 500 Internal Server Error – We had a problem with our server 服务器内部错误
  • 如果失败body带有错误描述信息