BitMEX / api-connectors

Libraries for connecting to the BitMEX API.
https://www.bitmex.com/app/restAPI
910 stars 795 forks source link

"Signature not valid" When using Filter in C# Rest API #299

Open Spino1981 opened 5 years ago

Spino1981 commented 5 years ago

I am using your code from https://github.com/BitMEX/api-connectors/blob/master/official-http/csharp/BitMEXAPI.cs public string GetOrders() { var param = new Dictionary<string, string>(); param["symbol"] = "XBTUSD"; //param["filter"] = "{\"open\":true}"; //param["columns"] = ""; //param["count"] = 100.ToString(); //param["start"] = 0.ToString(); //param["reverse"] = false.ToString(); //param["startTime"] = ""; //param["endTime"] = ""; return Query("GET", "/order", param, true); }

When I try to use a filter in a GET request I get the following {"error":{"message":"Signature not valid.","name":"HTTPError"}}

This is happening for example if I try to filter a GET /position public string GetPosition(string symbol) { var param = new Dictionary<string, string>(); //param["filter"] = String.Concat("{\"symbol\":\"", symbol, "\"}"); return Query("GET", "/position", param, true); }

Or in a GET /order `public string GetOpenOrders(string symbol) { var param = new Dictionary<string, string>(); param["symbol"] = symbol; param["filter"] = "{\"open\":true}"; return Query("GET", "/order", param, true); }

`

The API Keys seems to be working fine as I can retrieve other information using for example the following public string GetWallet(string symbol) { var param = new Dictionary<string, string>(); param["symbol"] = symbol; return Query("GET", "/user/wallet", param, true); }

Also the code works if I comment out the "filter" line, the issue seems to be affecting when a filter is involved

Thank you in advance

Spino1981 commented 5 years ago

Looks like these lines don't escape the (:) colon in the filter query string url = "/api/v1" + function + ((method == "GET" && paramData != "") ? "?" + paramData : "");

HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create("https://" + _domain + url);

So if you try in the Rest API Explorer (https://www.bitmex.com/api/explorer/#!/Order/Order_getOrders) you get the query string escaped https://www.bitmex.com/api/v1/order?symbol=XBTUSD&filter=%7B%22open%22%3Atrue%7D while the query string sent from the code is https://www.bitmex.com/api/v1/order?symbol=XBTUSD&filter=%7B%22open%22:true%7D

Still trying to find a way to escape that colon (:)

ddlau commented 5 years ago

escape that colon did solve nothing, check issue #305

Spino1981 commented 5 years ago

escape that colon did solve nothing, check issue #305

I see, so it is happening with another language as well, I still haven't been able to solve it

ddlau commented 5 years ago

escape that colon did solve nothing, check issue #305

I see, so it is happening with another language as well, I still haven't been able to solve it

i still do not know how to fix it in c#, but i've found the solution in python substitute 'url' in self.session.get method call to 'yarl.URL(url, encoded=True)' would fix it it seems that, even you inspect the url and found there is not ':' but '%3A', but the real url you're getting is the ':' version