问题描述
我尝试使用仅给定订单符号的formData发送取消订单请求。但不幸的是,所有其他订单也从交易所中删除。 我添加到请求的formData似乎不起作用。
在Dart语言中,是否有人对使用formData向服务器发送取消请求的正确方式有经验?
非常感谢您。
解决方法
我花了将近2天的时间来弄清楚这一点:)有一天,希望对像我这样需要帮助的人有用。
这是将取消订单发送到bitmex的我的解决方案...用飞镖语言。
您还可以使用此代码创建有效的签名和身份验证。
欢呼
var verb = "DELETE";
var path = "/api/v1/order";
var query = "orderID=$ordID";
if (query != "") path = path + "?" + query;
return await bitmexHttpService.authRequest(verb: verb,path: path);
Future<String> authRequest({String verb,String path,String data = ""}) async {
var _nonce = ((DateTime.now()).microsecondsSinceEpoch).toInt();
var _signature = signature(apiSecret,verb,path,_nonce);
var headerWithAuthCredentials = createHeader(verb,_nonce,_signature);
http.Response response;
try {
if (verb == "GET")
response = await http.get(baseUrl + path,headers: headerWithAuthCredentials);
else if (verb == "POST")
response = await http.post(baseUrl + path,body: data,headers: headerWithAuthCredentials);
else if (verb == "DELETE") {
response = await http.delete(baseUrl + path,headers: headerWithAuthCredentials);
}
if (response.statusCode == 200)
return response.body;
else {
var json = jsonDecode(response.body);
throw Failure(message: json["error"]["message"]);
}
} catch (e) {
print(e.toString());
}
}
Map<String,String> createHeader(String verb,int nonce,_signature) {
return {
"api-key": "$apiKey","api-expires": nonce.toString(),"api-signature": "${_signature.toString()}",'content-type': 'application/x-www-form-urlencoded;charset=utf-8','accept': 'application/json','x-Requested-With': 'XMLHttpRequest',};
}
signature(String apiSecret,String verb,int nonce) {
var message = verb + path + nonce.toString();
var messageEncoded = utf8.encode(message.trim());
var apiSecretEncoded = utf8.encode(apiSecret);
var apiSecretAshMacSha256 = new Hmac(sha256,apiSecretEncoded);
var sign = apiSecretAshMacSha256.convert(messageEncoded);
return sign;
}