Thread: Trouble with POST using RestSharp -Rest api

  1. #1
    Registered User
    Join Date
    Nov 2015
    Posts
    2

    Trouble with POST using RestSharp -Rest api

    I'm having some trouble getting the NewOrder api to work


    The Server Always Returns "Invalid" Api key , The Get Method Works !


    https://hitbtc.com/api
    post url: /api/1/trading/new_order?nonce=1395049771755&apikey=f6ab189hd7a20 07e01d95667de3c493d


    post data: clientOrderId=11111112&symbol=BTCUSD&side=buy&pric e=0.1&quantity=100&type=limit&timeInForce=GTC


    php example; https://gist.github.com/hitbtc-com/10885873
    sorce
    Code:
     using RestSharp;
    using System;
    using System.Linq;
    using System.Security.Cryptography;
    using System.Text;
    
    
    
    
    namespace Hitbtc.Api.Demo
    {
        class Program
        {
            static void Main(string[] args)
            {
                const string apiKey = "xxx";
                const string secretKey = "yyy";
                var client = new RestClient("https://api.hitbtc.com");
    
    
                var VARrequest = new RestRequest("/api/1/trading/new_order", Method.POST);
                VARrequest.AddParameter("nonce", GetNonce());
                VARrequest.AddParameter("apikey", apiKey);
                string sign = CalculateSignature(client.BuildUri(VARrequest).PathAndQuery, secretKey);
                VARrequest.AddHeader("X-Signature", sign);
                VARrequest.RequestFormat = DataFormat.Json;
                VARrequest.AddBody(new
                {
                  clientOrderId = "58f32654-723a-4b60-ad6b",
                  symbol = "BTCUSD",
                  side = "buy",
                  quantity = "0.01",
                  type = "limit" ,
                  price = "788.56",
                  timeInForce = "GTC" });
               
                var VARresponse = client.Execute(VARrequest);
    
    
                Console.WriteLine(VARresponse.Content);
                System.Threading.Thread.Sleep(5000);
            }
    
    
    
    
            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
                }
            }
        }
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    This is C#, I presume?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Nov 2015
    Posts
    2
    Quote Originally Posted by laserlight View Post
    This is C#, I presume?
    Yes its C# xD

  4. #4
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    post url: /api/1/trading/new_order?nonce=1395049771755&apikey=f6ab189hd7a20 07e01d95667de3c493d
    It looks like you have a space or some other non-printable character in your api key...
    If you understand what you're doing, you're not learning anything.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. HTTP POST Request trouble
    By omalee in forum C++ Programming
    Replies: 1
    Last Post: 04-22-2014, 08:40 PM
  2. post increment gives trouble !
    By samirself in forum C Programming
    Replies: 3
    Last Post: 05-07-2005, 03:11 PM
  3. get rid of rest
    By pode in forum Networking/Device Communication
    Replies: 1
    Last Post: 10-20-2003, 06:55 PM
  4. rest
    By doubleanti in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 01-29-2002, 05:32 PM
  5. HBITMAP and the rest...
    By DutchStud in forum Windows Programming
    Replies: 2
    Last Post: 12-02-2001, 10:42 PM