0

I'm trying to write a simple POST request (returnBalances), but it always returns error message.

Can't figure out what I'm doing wrong.

Solved the problem. The code is fixed to work now.

Here is the code:

    public String checkBalancesRequest()
    {
        uint Nonce = requestCounter++;
        String Url = "https://poloniex.com/tradingApi";
        HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, Url);

        Dictionary<string,string> body= new Dictionary<string,string>();
        body.Add("command", "returnBalances");
        body.Add("nonce",Nonce.ToString());

        HttpContent cont = new FormUrlEncodedContent(body);

        String stringToSign = cont.ReadAsStringAsync().Result;
        String signature = SHA512Sign(stringToSign, secretView);

        request.Content = cont;
        request.Headers.Add("Key", keyView);
        request.Headers.Add("Sign", signature);
        dynamic result2 = client.SendAsync(request).Result;
        String instrs = "";
        if (result2.IsSuccessStatusCode) { instrs = result2.Content.ReadAsStringAsync().Result.ToString(); if (instrs == "{\"error\":\"Invalid command.\"}") { instrs = null; } } else { instrs = null; }
        return instrs;
    }

    public static String SHA512Sign(String message, String key)
    {
        using (MemoryStream stream = new MemoryStream(Encoding.UTF8.GetBytes(message)))
        {
            var signed = new HMACSHA512(Encoding.UTF8.GetBytes(key)).ComputeHash(stream)
                .Aggregate(new StringBuilder(), (sb, b) => sb.AppendFormat("{0:x2}", b), (sb) => sb.ToString());
            return signed;
        }
    }
Der Der
  • 1
  • 2
  • Read the docs. API expects a **POST** parameters to be passed in, but you don't pass any parameter at all. Have you ever try to debug this? – vasily.sib Dec 25 '18 at 03:23
  • I did, I also had the following line: request.Content = new StringContent(plain, Encoding.UTF8); I passed different encoding to Content of the String plain. The docs are poorly written: All calls to the trading API are sent via HTTP POST to https://poloniex.com/tradingApi and must contain the following headers: Key - Your API key. Sign - The query's POST data signed by your key's "secret" according to the HMAC-SHA512 method. Additionally, all queries must include a "nonce" POST parameter. The nonce parameter is an integer which must always be greater than the previous nonce used. – Der Der Dec 25 '18 at 14:02
  • You assign some values to variable `plain` and you use it only to calculate signature, but you never pass it to your request body. – vasily.sib Dec 26 '18 at 02:56
  • I just wrote that I had a line request.Content = new StringContent(plain, Encoding.UTF8); isn't it exactly the passing to the request body? – Der Der Dec 26 '18 at 13:20
  • sorry, I just figured out how to edit the body of the question, it is my first post on here – Der Der Dec 26 '18 at 13:30
  • another issue, that I can see in your code is that you set **Accept** header instead of **Content-Type**. Also, you set your `request.Content = new StringContent(...)` which set Content-Type to `text/plain`. You may use [HttpFormUrlEncodedContent](https://learn.microsoft.com/en-us/uwp/api/windows.web.http.httpformurlencodedcontent) instead. – vasily.sib Dec 27 '18 at 03:12
  • also, can you use [HttpClient](https://learn.microsoft.com/en-us/aspnet/web-api/overview/advanced/calling-a-web-api-from-a-net-client#install-the-web-api-client-libraries) with extensions from [Microsoft.AspNet.WebApi.Client](https://www.nuget.org/packages/Microsoft.AspNet.WebApi.Client/) nuget package? It is far more straightforward. – vasily.sib Dec 27 '18 at 03:18

1 Answers1

0

the code is fixed to work now. I added the HttpContent object to explicitly define the body of the request

Der Der
  • 1
  • 2