简体   繁体   中英

RestSharp: POST request in demo-api.hitbtc.com doesn't work

I want to create a bot for demo-api.hitbtc.com. All GET request works great.

using System;
using System.Security.Cryptography;
using RestSharp;
using System.Linq;
using System.Text;

static void Main(string[] args)
    {
        const string apiKey = "xxx";
        const string secretKey = "xxx";

        var client1 = new RestClient("http://demo-api.hitbtc.com");
        var request1 = new RestRequest("/api/1/trading/new_order", Method.POST);
        request1.AddParameter("nonce", GetNonce().ToString());
        request1.AddParameter("apikey", apiKey);

        string sign1 = CalculateSignature(client1.BuildUri(request1).PathAndQuery, secretKey);
        request1.AddHeader("X-Signature", sign1);

        request1.RequestFormat = DataFormat.Json;
        request1.AddBody(new
        {
            clientOrderId = "58f32654723a4b60ad6b",
            symbol = "BTCUSD",
            side = "buy",
            quantity = "0.01",
            type = "market",
            timeInForce = "GTC"
        });

        var response1 = client1.Execute(request1);

        Console.WriteLine(response1.Content);
        Console.ReadLine();
}

        private static long GetNonce()
    {
        return DateTime.Now.Ticks * 10 / TimeSpan.TicksPerMillisecond; // use millisecond timestamp or whatever you want
    }

    public static string CalculateSignature(string text, string secretKey)
    {
        using (var hmacsha512 = new HMACSHA512(Encoding.UTF8.GetBytes(secretKey)))
        {
            hmacsha512.ComputeHash(Encoding.UTF8.GetBytes(text));
            return string.Concat(hmacsha512.Hash.Select(b => b.ToString("x2")).ToArray()); // minimalistic hex-encoding and lower case
        }
    }

But when I want to try POST request, I got this error:

{"code":"InvalidContent","message":"Missing apikey parameter"}

In hitbtc.com API Documentation has said that: "Each request should include the following parameters: nonce, apikey, signature". Where is the problem?

It appears RestSharp removes query string parameters by default when performing POST operations. To work around this, you will need to tell it that your parameters are intended to be query string parameters:

request1.AddQueryParameter("nonce", GetNonce().ToString());
request1.AddQueryParameter("apikey", apiKey);

instead of using reqest1.AddParameter(name, value)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM